Android 資料存放區之 FileInputStream 工具類及FileInputStream類的使用_Android

來源:互聯網
上載者:User

安卓的三種本地的典型資料存放區方式

SharedPreferences

以檔案格式儲存在本機存放區中

SQL資料庫

這篇文章就是講解一下如何使用 SharedPreferences 儲存檔案。主要解釋什麼都寫在注釋裡面的。

IDE : Android Studio

參考文章:http://www.jb51.net/article/74215.htm

絮叨一下:本來檔案操作這一塊上周就想把其弄懂,然後繼續進一步的學習。但是因為官方的 Android Training 之中的概念太過於繁雜。導致我認為儲存到內部之中要查詢空間,得到返回之類。結果是因為我把儲存在內部空間(data目錄下的包名下的file檔案夾)與外部空間(儲存空間)的概念混淆。所以耽誤了大量時間並且不得法。最後還是看到參考文章才知道應該怎麼去寫。然後自己跟著參考文章過了一遍。

同樣的,我採用了分離寫法,也就是建立一個工具類,模組化方便使用。希望能協助到別人,也是給自己建立一種概念。

話不多說,上代碼:

import android.content.Context;import android.util.Log;import java.io.FileInputStream;import java.io.FileOutputStream;/*** Created by zhizhao on 2015/11/1 0001 in 16:00.*/public class UsingFileInputStream {private Context fileContext;private String fileName;private String fileUserName;private String filePassword;public UsingFileInputStream(String name, Context context, String userName, String password) {this.fileName = name;this.fileContext = context;this.fileUserName = userName;this.filePassword = password;}//儲存的時候是在檔案內容中連續寫入,也就是在之前儲存的資料基礎上再次寫入。public void writeFileInputStream() {try {FileOutputStream fileOutputStream = fileContext.openFileOutput(fileName,fileContext.MODE_PRIVATE);byte[] byteUserName = fileUserName.getBytes();byte[] bytePassword = filePassword.getBytes();fileOutputStream.write(byteUserName);fileOutputStream.write(bytePassword);Log.v("FileInputStream儲存結果 ", "UserName = " + fileUserName + " Password = " + filePassword);fileOutputStream.flush();fileOutputStream.close();} catch (Exception e) {e.printStackTrace();}}//讀取檔案是把整個檔案的內容進行讀取。如果要加入解析,則儲存的時候儲存為特殊格式。public void readFileInputStream() {try {FileInputStream fileInputStream = fileContext.openFileInput(fileName);int len = fileInputStream.available();byte[] buffer = new byte[len];fileInputStream.read(buffer);Log.v("讀取到的檔案為:", ""+new String(buffer));fileInputStream.close();} catch (Exception e) {e.printStackTrace();}}}

下面是使用:

private void writeFile(){UsingFileInputStream fileInputStream = new UsingFileInputStream("account",MySaveDataActivity.this, userName, userPass);fileInputStream.writeFileInputStream();tvReadInformation.setText("儲存成功!" + "\n UserName = " + userName + "\n UserPass = " + userPass);}private void readFile(){UsingFileInputStream fileInputStream = new UsingFileInputStream("account",MySaveDataActivity.this, userName, userPass);fileInputStream.readFileInputStream();tvReadInformation.setText("讀取成功!"+"\n UserName = "+userName+"\n UserPass = "+userPass);}

總結一下:

我覺得自己目前寫的也不是很對,很完美。因為在調用過程中要反覆去填寫檔案名稱,傳入值。並且在傳回值之中很難知道成功失敗。

況且我並沒有把檔案異常捕獲並進行操作,因為如果是沒有檔案的情況下去操作的話,必然會報錯:null 指標異常。

不過既然是練習也就沒考慮那麼多,因為這種時候只要在 try{}catch(){} 代碼塊裡面加入自己的操作手段就可以了。

下面還有點時間接著給大家介紹Android FileInputStream類的使用

1.FileInputStream類概述

    繼承關係:

       java.io.FileInputStream->java.io.InputStream->java.lang.Object

實現介面:

       Closeable    

 類的功能:

    FileInputStream 從檔案系統中的某個檔案中擷取輸入位元組。哪些檔案可用取決於主機環境。

       FileInputStream 用於讀取諸如映像資料之類的原始位元組流。要讀取字元流,請考慮使用 FileReader。

2.類的屬性和行為

   (1) public void close() throws IOException

    功能:  關閉此檔案輸入資料流並釋放與此流有關的所有系統資源。

            如果此流有一個與之關聯的通道,則關閉該通道。

    指定者:介面 Closeable 中的 close

    覆蓋:  類 InputStream 中的 close

    拋出:  IOException - 如果發生 I/O 錯誤。

   (2) public int read() throws IOException

    功能:  從此輸入資料流中讀取一個資料位元組。如果沒有輸入可用,則此方法將阻塞。

    指定者:類 InputStream 中的 read

    返回:  下一個資料位元組;如果已到達檔案末尾,則返回 -1。

    拋出:  IOException - 如果發生 I/O 錯誤。

   (3) public int read(byte[] b) throws IOException

    功能:從此輸入資料流中將最多b.length個位元組的資料讀入一個位元組數組中。在某些輸入可用前,此方法將阻塞

    覆蓋:類 InputStream 中的 read

    參數:b - 儲存讀取資料的緩衝區

    返回:讀入緩衝區的位元組總數,如果因為已經到達檔案末尾而沒有更多的資料,則返回 -1。

    拋出:IOException - 如果發生 I/O 錯誤。

   (4) public int read(byte[] b, int off, int len) throws IOException

    功能:從此輸入資料流中將最多len個位元組的資料讀入一個位元組數組中。在某些輸入可用之前,此方法將阻塞。

    覆蓋:類 InputStream 中的 read

    參數:b - 儲存讀取資料的緩衝區。

          off - 資料的起始位移量。

          len - 讀取的最大位元組數。

    返回:讀入緩衝區的位元組總數,如果因為已經到達檔案末尾而沒有更多的資料,則返回 -1。

    拋出:IOException - 如果發生 I/O 錯誤。

3.常見錯誤

   在eclipse下使用FileInputStream,提示找不到指定檔案

   代碼:

 filename = "abc.txt" ;  FileInputStream fis = new FileInputStream(filename);

   錯誤顯示:

 java.io.FileNotFoundException: dbconfig.properties (系統找不到指定的檔案。)  at java.io.FileInputStream.open(Native Method)  at java.io.FileInputStream.<init>(FileInputStream.java:106)  at java.io.FileInputStream.<init>(FileInputStream.java:66)

   解決方案:

       因為eclipse下運行main程式時,eclipse會自動將發布目錄作為其根目錄,所以會提示找不到檔案,將filename改為絕對目錄即可

      filename = "\sdcard\...\abc.txt" ;

聯繫我們

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