標籤:
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/ 目錄下的
第二個是檔案的操作模型,主要有兩種模式可選
- MODE_PRIVATE(覆蓋,是預設的操作模式)
- MODE_APPEND (追加) 不存在建立
- MODE_WORLD_READABLE 允許其他應用程式對程式進行讀寫操作.不過太危險了 ,在 4.2 之後 被廢棄
- MODE_WORLD_WRITEABLE
edit.setSelection(inputText.length());
TextUtils.isEmpty()方法, 這個方法很好用,可以同時判斷兩種狀態(null 和 等於Null 字元串) 的時候,就會返回true
Android--檔案儲存體操作(一)