下邊總結一下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裝置。它可以跳轉到檔案的任意位置,從目前位置開始讀寫。
5、InputStream和FileInputStream都可以使用skip和read(buffre,offset,length)函數來實作檔案的随機讀取。