android中常用的讀取檔案的用法如下

2021-09-08 18:12:23 字數 1914 閱讀 8451

string res = "";

trycatch(exception e)

string filename = "test.txt"; //檔案名字

string res="";

trycatch(exception e)

//寫資料

public void writefile(string filename,string writestr) throws ioexceptioncatch(exception e) //讀資料 public string readfile(string filename) throws ioexceptioncatch(exception e) return res; }

//寫資料到sd中的檔案

public void writefilesdcardfile(string filename,string write_str) throws ioexceptioncatch(exception e) } //讀sd中的檔案 public string readfilesdcardfile(string filename) throws ioexceptioncatch(exception e) return res; }

//讀檔案

public string readsdfile(string filename) throws ioexception //寫檔案 public void writesdfile(string filename, string write_str) throws ioexception

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

android 讀取檔案

日期 2011 02 23 字型 大中小 在研究拍照後突破的儲存路徑的問題,開始儲存路徑寫死為 private string folder sdcard dcim camera sd卡上拍照程式的儲存路徑 後來發現這樣寫雖然一般不會出錯,但不是很好,因為不同相機,可能路徑會出問題。較好的方法是通過e...

Android中常用的設計模式

下面介紹一些android開發中常用的幾種設計模式 觀察者模式定義了一種一對多的依賴關係,當乙個物件的狀態改變時,所有依賴於它的物件都能得到通知並被自動更新。適用場景 1,天氣預報服務,一旦有天氣資訊更新,它就會及時向你傳送最新的天氣資訊 2,雜誌的訂閱 3,廣播 4,事件匯流排eventbus 確...

android中常用的工具類

public class densityutil 根據手機的解析度從 dp 的單位 轉成為 px 畫素 param dpvalue 虛擬畫素 return 畫素 public static int dp2px float dpvalue 根據手機的解析度從 px 畫素 的單位 轉成為 dp para...