檔案的讀寫【安卓8】

來源:互聯網
上載者:User

標籤:操作   讀寫   tput   檔案的   ble   資料   try   catch   模式   

      Android的I/O方法

方法

描述

public FileInputStream openFileInput(String name)

設定要開啟的檔案輸入資料流

public FileOutputStream openFileOutput(

      String name,int mode)

設定要開啟的檔案輸出資料流,指定操作模式,可以是0、MODE_APPEND、MODE_PRIVATE、MODE_WORLD_READABLE、MODE_WORLD_WRITEABLE

public Resources getResources()

返回Resources對象

       

 

檔案的讀寫權限

    openFileOutPut(String fliename,int model)

  儲存資料至指定檔案名稱,第二個參數是檔案的操作模式,有以下四個常量值:

  •   MODE_PRIVATE:設定檔案屬性為私人,只能被當前項目讀寫
  •   MODE_APPEND:以添加方式開啟檔案,並向檔案中添加資料
  •   MODE_WORLD_READABLE:該檔案可被其他項目讀取
  •   MODE_WORLD_WRITEABLE: 該檔案可被其他項目寫入

  

 儲存(寫入)檔案的操作

//讀取檔案的操作FileOutputStream fos=null;    try {        fos=openFileOutput("test.txt", Activity.MODE_PRIVATE);    } catch (FileNotFoundException e) {        e.printStackTrace();    }PrintWriter out=new PrintWriter(fos);out.print("白日依山盡");out.print("黃河入海流");out.close();
儲存檔案的操作

   

  讀取檔案的操作

//讀取檔案的操作try {    FileInputStream fis=openFileInput("test.txt");    BufferedReader br=new BufferedReader(new InputStreamReader(fis));    String str=null;    while((str=br.readLine())!=null){        TextView tv=(TextView) findViewById(R.id.TV);        tv.setText(str);    }} catch (FileNotFoundException e) {    e.printStackTrace();} catch (IOException e) {    e.printStackTrace();}
讀取檔案的操作

 

檔案的讀寫【安卓8】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.