一、 从resource中的raw文件夹中获取文件并读取数据(资源文件只能读不能写)
1 String res = ""; 2 3 try{ 4 5 InputStream in = getResources().openRawResource(R.raw.bbi); 6 7 //在\Test\res\raw\bbi.txt, 8 9 int length = in.available();10 11 byte [] buffer = new byte[length];12 13 in.read(buffer);14 15 //res = EncodingUtils.getString(buffer, "UTF-8");16 17 //res = EncodingUtils.getString(buffer, "UNICODE");18 19 res = EncodingUtils.getString(buffer, "BIG5");20 21 //依bbi.txt的编码类型选择合适的编码,如果不调整会乱码22 23 in.close();24 25 }catch(Exception e){26 27 e.printStackTrace();28 29 }
myTextView.setText(res);//把得到的内容显示在TextView上
二、 从asset中获取文件并读取数据(资源文件只能读不能写)
1 String fileName = "yan.txt"; //文件名字 2 3 String res=""; 4 5 try{ 6 7 InputStream in = getResources().getAssets().open(fileName); 8 9 // \Test\assets\yan.txt这里有这样的文件存在10 11 int length = in.available();12 13 byte [] buffer = new byte[length];14 15 in.read(buffer);16 17 res = EncodingUtils.getString(buffer, "UTF-8");18 19 }catch(Exception e){20 21 e.printStackTrace();22 23 }
三、 从sdcard中去读文件,首先要把文件通过\android-sdk-windows\tools\adb.exe把本地计算机上的文件copy到sdcard上去,adb.exe push e:/Y.txt /sdcard/, 不可以用adb.exe push e:\Y.txt \sdcard\ 同样: 把仿真器上的文件copy到本地计算机上用: adb pull ./data/data/com.tt/files/Test.txt e:/
1 String fileName = "/sdcard/Y.txt"; 2 3 //也可以用String fileName = "mnt/sdcard/Y.txt"; 4 5 String res=""; 6 7 try{ 8 9 FileInputStream fin = new FileInputStream(fileName);10 11 //FileInputStream fin = openFileInput(fileName);12 13 //用这个就不行了,必须用FileInputStream14 15 int length = fin.available();16 17 byte [] buffer = new byte[length];18 19 fin.read(buffer);20 21 res = EncodingUtils.getString(buffer, "UTF-8");22 23 fin.close();24 25 }catch(Exception e){26 27 e.printStackTrace();28 29 }30 31 myTextView.setText(res);
四、 写文件, 一般写在\data\data\com.test\files\里面,打开DDMS查看file explorer是可以看到仿真器文件存放目录的结构的
1 String fileName = "TEST.txt"; 2 3 String message = "FFFFFFF11111FFFFF" ; 4 5 writeFileData(fileName, message); 6 7 public voidwriteFileData(String fileName,String message){ 8 9 try{10 11 FileOutputStream fout =openFileOutput(fileName, MODE_PRIVATE);12 13 byte [] bytes = message.getBytes();14 15 fout.write(bytes);16 17 fout.close();18 19 }20 21 catch(Exception e){22 23 e.printStackTrace();24 25 }26 27 }
五、 写, 读data/data/目录(相当AP工作目录)上的文件,用openFileOutput
1 //写文件在./data/data/com.tt/files/下面 2 3 public voidwriteFileData(String fileName,String message){ 4 5 try{ 6 7 FileOutputStream fout =openFileOutput(fileName, MODE_PRIVATE); 8 9 byte [] bytes = message.getBytes();10 11 fout.write(bytes);12 13 fout.close();14 15 }16 17 catch(Exception e){18 19 e.printStackTrace();20 21 }22 23 }24 25 //-------------------------------------------------------26 27 //读文件在./data/data/com.tt/files/下面28 29 public String readFileData(String fileName){30 31 String res="";32 33 try{34 35 FileInputStream fin = openFileInput(fileName);36 37 int length = fin.available();38 39 byte [] buffer = new byte[length];40 41 fin.read(buffer);42 43 res = EncodingUtils.getString(buffer, "UTF-8");44 45 fin.close();46 47 }48 49 catch(Exception e){50 51 e.printStackTrace();52 53 }54 55 return res;56 57 }
六、 写, 读sdcard目录上的文件,要用FileOutputStream, 不能用openFileOutput
1 //写在/mnt/sdcard/目录下面的文件 2 3 public voidwriteFileSdcard(String fileName,String message){ 4 5 try{ 6 7 //FileOutputStream fout = openFileOutput(fileName, MODE_PRIVATE); 8 9 FileOutputStream fout = newFileOutputStream(fileName);10 11 byte [] bytes = message.getBytes();12 13 fout.write(bytes);14 15 fout.close();16 17 }18 19 catch(Exception e){20 21 e.printStackTrace();22 23 }24 25 }26 27 //读在/mnt/sdcard/目录下面的文件28 29 public String readFileSdcard(String fileName){30 31 String res="";32 33 try{34 35 FileInputStream fin = new FileInputStream(fileName);36 37 int length = fin.available();38 39 byte [] buffer = new byte[length];40 41 fin.read(buffer);42 43 res = EncodingUtils.getString(buffer, "UTF-8");44 45 fin.close();46 47 }48 49 catch(Exception e){50 51 e.printStackTrace();52 53 }54 55 return res;56 57 }
注: openFileOutput是在raw里编译过的,FileOutputStream是任何文件都可以
Android读写文件汇总 http://www.ziyouku.com/archives/android-to-read-and-write-file-summary.html
时间: 2025-01-27 20:01:18