Android - 文件操作 小結
在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);
res = EncodingUtils.getString(buffer, "UTF-8");
}catch(Exception e){
e.printStackTrace();
}
二、讀寫/data/data/<應用程序名>目錄上的文件:
//寫數據
public void writeFile(String fileName,String writestr{
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){
String res="";
try{
FileInputStream fin = openFileInput(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;
} 三、讀寫SD卡中的文件。也就是/mnt/sdcard/目錄下面的文件 :
//寫數據到SD中的文件
public void writeFileSdcardFile(String fileName,String write_str){
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){
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;
} 四、使用File類進行文件的讀寫:
//讀文件
public String readSDFile(String fileName) {
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){
File file = new File(fileName);
FileOutputStream fos = new FileOutputStream(file);
byte [] bytes = write_str.getBytes();
fos.write(bytes);
fos.close();
} 五、另外,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(); //刪除文件夾或文件
六、使用RandomAccessFile進行文件的讀寫:
RandomAccessFile的使用方法比較靈活,功能也比較多,可以使用類似seek的方式可以跳轉到文件的任意位置,從文件指示器當前位置開始讀寫。
它有兩種構造方法
new RandomAccessFile(f,"rw");//讀寫方式
new RandomAccessFile(f,"r");//只讀方式
使用事例:
/*
* 程序功能:演示了RandomAccessFile類的操作,同時實現了一個文件復制操作。
*/
package com.lwj.demo;
import java.io.*;
public class RandomAccessFileDemo {
public static void main(String[] args) throws Exception {
RandomAccessFile file = new RandomAccessFile("file", "rw");
// 以下向file文件中寫數據
file.writeInt(20);// 占4個字節
file.writeDouble(8.236598);// 占8個字節
file.writeUTF("這是一個UTF字符串");// 這個長度寫在當前文件指針的前兩個字節處,可用readShort()讀取
file.writeBoolean(true);// 占1個字節
file.writeShort(395);// 占2個字節
file.writeLong(2325451l);// 占8個字節
file.writeUTF("又是一個UTF字符串");
file.writeFloat(35.5f);// 占4個字節
file.writeChar('a');// 占2個字節
file.seek(0);// 把文件指針位置設置到文件起始處
// 以下從file文件中讀數據,要注意文件指針的位置
System.out.println("——————從file文件指定位置讀數據——————");
System.out.println(file.readInt());
System.out.println(file.readDouble());
System.out.println(file.readUTF());
file.skipBytes(3);// 將文件指針跳過3個字節,本例中即跳過了一個boolean值和short值。
System.out.println(file.readLong());
file.skipBytes(file.readShort()); // 跳過文件中“又是一個UTF字符串”所占字節,注意readShort()方法會移動文件指針,所以不用加2。
System.out.println(file.readFloat());
//以下演示文件復制操作
System.out.println("——————文件復制(從file到fileCopy)——————");
file.seek(0);
RandomAccessFile fileCopy=new RandomAccessFile("fileCopy","rw");
int len=(int)file.length();//取得文件長度(字節數)
byte[] b=new byte[len];
file.readFully(b);
fileCopy.write(b);
System.out.println("復制完成!");
}
} 總結:
1、apk中有兩種資源文件,使用兩種不同的方式進行打開使用。
raw使用InputStream in = getResources().openRawResource(R.raw.test);
asset使用InputStream in = getResources().getAssets().open(fileName);
這些數據只能讀取,不能寫入。
2、SD卡中的文件使用FileInputStream和FileOutputStream進行文件的操作。
3、存放在數據區(/data/data/..)的文件只能使用openFileOutput和openFileInput進行操作。
注意不能使用FileInputStream和FileOutputStream進行文件的操作。
4、RandomAccess類僅限于文件的操作,不能訪問其他IO設備。它可以跳轉到文件的任意位置,從當前位置開始讀寫。
轉自:http://blog.csdn.net/ztp800201/article/details/7322110
本文由用戶 openkk 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!