Android--檔案儲存體操作(一)

來源:互聯網
上載者:User

標籤:

1、寫入內建空間      output   write

2、寫入SDCard卡      input   read

該檔案指標可以通過 getFilePointer 方法讀取,並通過 seek 方法設定。

RandomAccessFile同時將FileInputStream和FileOutputStream整合到一起,而且支援將從檔案任意位元組處讀或寫資料,RandomAccessFile類提供一種機制,相當於在檔案流中插入了一個指標,可以按需讀取。

File只是對一個檔案或目錄的抽象,可以想象成是一個檔案控制代碼、標識,這個類本身只提供對檔案的開啟,關閉,刪除,屬性訪問等等;

檔案儲存體簡單的來說就是一般的JAVASE中的IO流,只是把他應用於Android手機中而已;

1  檔案儲存體

核心就是Context的openFileInput()和openFileOutput()方法.之後就用Java流來進行讀寫就可一了

    //將一段常值內容儲存到檔案中:    //在onDestory() 裡面調用save()方法.此方法寫在了super.onDestory() 下面.
  //openFileOutput --> FileOutputStream --> OutputStreamReader --> BufferedReader
public void save() { String data = "Data to save"; FileOutputStream out = null; BufferedWriter writer = null; try { out = mContext.openFileOutput("data", Context.MODE_PRIVATE);//需要FileNot錯誤. writer = new BufferedWriter(new OutputStreamWriter(out)); //構建一個outputStreamWriter.接著由構建了一個BufferedWriter() writer.write(data);//需要IO錯誤 } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (writer != null) { try { writer.close(); } catch (IOException e) { e.printStackTrace(); } } } } //在下一次開啟的時候,從檔案中讀取資料
  // openFileinput --> FileInputStream --> InputStreamReader --> BufferedReader
  // 前兩個 file 一直在 input 前面 public String load() { FileInputStream in = null; BufferedReader reader = null; StringBuilder sb = new StringBuilder(); try { in = mContext.openFileInput("data"); //需要fileNOt錯誤 reader = new BufferedReader(new InputStreamReader(in)); String line = ""; while ((line = reader.readLine()) != null) { //需要IO錯誤 sb.append(line); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } return sb.toString(); }

 

 

 

 

//寫入到內建儲存空間    public void write(View view) {        String inputString = inputText.getText().toString();        if (inputString == null || "".equals(inputString.trim())) {            Toast.makeText(mContext, "請輸入需要寫入的內容,不可為空", Toast.LENGTH_SHORT).show();            return;        }        FileOutputStream fos = null;        try {            if (appendBtn.isChecked()) {                fos = mContext.openFileOutput(fileName, Context.MODE_APPEND);            } else {                fos = mContext.openFileOutput(fileName, Context.MODE_PRIVATE);            }            fos.write(inputString.getBytes());      //向檔案裡寫入內容            Toast.makeText(mContext, "內容寫入完畢!", Toast.LENGTH_SHORT).show();        } catch (FileNotFoundException e) {            Toast.makeText(mContext, "檔案沒有找到,請檢查檔案路徑", Toast.LENGTH_SHORT).show();        } catch (IOException e) {            Toast.makeText(mContext, "寫入過程中出現錯誤 IO", Toast.LENGTH_SHORT).show();        }    }    //讀取內建儲存空間    public void readFromSDCard(View view) {        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {            String path = Environment.getExternalStorageDirectory() + File.separator + fileName;            File file = new File(path);            StringBuilder sBuilder = new StringBuilder("");            byte[] buffer = new byte[256];            int hasRead = 0;            try {                RandomAccessFile raf = new RandomAccessFile(file, "rw");                while ((hasRead = raf.read(buffer)) != -1) {                    sBuilder.append(new String(buffer, 0, hasRead));                }                outputText.setText(sBuilder);                raf.close();            } catch (FileNotFoundException e) {                Toast.makeText(mContext, "檔案沒有找到,請檢查檔案路徑", Toast.LENGTH_SHORT).show();            } catch (IOException e) {                Toast.makeText(mContext, "寫入過程中出現錯誤 IO", Toast.LENGTH_SHORT).show();            }        }    }

 

 

預設儲存在 /data/data/<packagename>/files/ 目錄下的

第二個是檔案的操作模型,主要有兩種模式可選

  1. MODE_PRIVATE(覆蓋,是預設的操作模式)     
  2. MODE_APPEND (追加)   不存在建立
  3. MODE_WORLD_READABLE    允許其他應用程式對程式進行讀寫操作.不過太危險了 ,在 4.2 之後 被廢棄
  4. MODE_WORLD_WRITEABLE  

 

edit.setSelection(inputText.length());

TextUtils.isEmpty()方法,  這個方法很好用,可以同時判斷兩種狀態(null  和  等於Null 字元串)  的時候,就會返回true 

 

Android--檔案儲存體操作(一)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.