Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發實例 >> Android 數據存儲之 FileInputStream 工具類及FileInputStream類的使用

Android 數據存儲之 FileInputStream 工具類及FileInputStream類的使用

編輯:Android開發實例

安卓的三種本地的典型數據存儲方式

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);
}

總結一下:

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

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

不過既然是練習也就沒考慮那麼多,因為這種時候只要在 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" ;

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved