android中常用的读取文件的用法如下

1. 从resource的raw中读取文件数据:

String res = "";

try{

//得到资源中的Raw数据流

InputStream in = getResources().openRawResource(R.raw.test);

//得到数据的大小

int length = in.available();

byte [] buffer = new byte[length];

//读取数据

in.read(buffer);

//依test.txt的编码类型选择合适的编码,如果不调整会乱码

res = EncodingUtils.getString(buffer, "BIG5");

//关闭

in.close();

}catch(Exception e){

e.printStackTrace();

}

2. 从resource的asset中读取文件数据

String fileName = "test.txt"; //文件名字

String res="";

try{

//得到资源中的asset数据流

InputStream in = getResources().getAssets().open(fileName);

int length = in.available();

byte [] buffer = new byte[length];

in.read(buffer);

in.close();

res = EncodingUtils.getString(buffer, "UTF-8");

}catch(Exception e){

e.printStackTrace();

}

3. 读写/data/data/<应用程序名>目录上的文件:

//写数据

public void writeFile(String fileName,String writestr) throws IOException{

try{

FileOutputStream fout =openFileOutput(fileName, MODE_PRIVATE);

byte [] bytes = writestr.getBytes();

fout.write(bytes);

fout.close();

}catch(Exception e){

e.printStackTrace();

}
//读数据

public String readFile(String fileName) throws IOException{

String res="";

try{

FileInputStream fin = new FileInputStream(jsonFlie);

int length = fin.available();

byte [] buffer = new byte[length];

fin.read(buffer);

res = EncodingUtils.getString(buffer, "UTF-8");

fin.close();

}catch(Exception e){

e.printStackTrace();

}

return res;

}

4. 读写SD卡中的文件。也就是/mnt/sdcard/目录下面的文件

//写数据到SD中的文件

public void writeFileSdcardFile(String fileName,String write_str) throws IOException{

try{

FileOutputStream fout = new FileOutputStream(fileName);

byte [] bytes = write_str.getBytes();

fout.write(bytes);

fout.close();

}catch(Exception e){

e.printStackTrace();

}

}
//读SD中的文件

public String readFileSdcardFile(String fileName) throws IOException{

String res="";

try{

FileInputStream fin = new FileInputStream(fileName);

int length = fin.available();

byte [] buffer = new byte[length];

fin.read(buffer);

res = EncodingUtils.getString(buffer, "UTF-8");

fin.close();

}catch(Exception e){

e.printStackTrace();

}

return res;

}

5. 使用File类进行文件的读写:

//读文件

public String readSDFile(String fileName) throws IOException {

File file = new File(fileName);

FileInputStream fis = new FileInputStream(file);

int length = fis.available();

byte [] buffer = new byte[length];

fis.read(buffer);

res = EncodingUtils.getString(buffer, "UTF-8");

fis.close();

return res;

}

//写文件

public void writeSDFile(String fileName, String write_str) throws IOException{

File file = new File(fileName);

FileOutputStream fos = new FileOutputStream(file);

byte [] bytes = write_str.getBytes();

fos.write(bytes);

fos.close();

}

  
6. [代码]五、另外,File类还有下面一些常用的操作:

String Name = File.getName();  //获得文件或文件夹的名称:

String parentPath = File.getParent();  //获得文件或文件夹的父目录

String path = File.getAbsoultePath();//绝对路经

String path = File.getPath();//相对路经

File.createNewFile();//建立文件

File.mkDir(); //建立文件夹

File.isDirectory(); //判断是文件或文件夹

File[] files = File.listFiles();  //列出文件夹下的所有文件和文件夹名

File.renameTo(dest);  //修改文件夹和文件名

File.delete();  //删除文件夹或文件 
 

1、apk中有两种资源文件,使用两种不同的方式进行打开使用。 
raw使用InputStream in = getResources().openRawResource(R.raw.test);

asset使用InputStream in = getResources().getAssets().open(fileName);

这些数据只能读取,不能写入。更重要的是该目录下的文件大小不能超过1M。

同时,需要注意的是,在使用InputStream的时候需要在函数名称后加上throws IOException。

2、SD卡中的文件使用FileInputStream和FileOutputStream进行文件的操作。

3、存放在数据区(/data/data/..)的文件只能使用openFileOutput和openFileInput进行操作。

注意不能使用FileInputStream和FileOutputStream进行文件的操作。

4、RandomAccessFile类仅限于文件的操作,不能访问其他IO设备。它可以跳转到文件的任意位置,从当前位置开始读写。

摘自 T9Team

原文地址:https://www.cnblogs.com/zl1991/p/5395478.html