下邊總結一下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)函數來實現文件的隨機讀取。