[Android進階]如何使用檔案來儲存程式中的資料

來源:互聯網
上載者:User

在程式中,有很多儲存和擷取資料的方法,本篇文章,主要介紹使用檔案系統對程式中的資料進行儲存和讀取的操作

 

我直接寫了一個協助類,進行檔案的寫入和讀取操作

 

 

/** * 用於在檔案中儲存程式資料 *  * @author zhaokaiqiang *  */public class FileHelper {private static final String TAG = "FileHelper";private Context mContext;FileHelper(Context _mContext) {mContext = _mContext;}// 在手機本地硬碟中儲存資訊public void save(String fileName, String content) {FileOutputStream fileOutputStream = null;try {fileOutputStream = mContext.openFileOutput(fileName,Context.MODE_PRIVATE);fileOutputStream.write(content.getBytes());} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {if (fileOutputStream != null) {fileOutputStream.close();}} catch (IOException e) {e.printStackTrace();}}}// 讀取手機硬碟中儲存的檔案public void read(String fileName) {FileInputStream fileInputStream = null;try {fileInputStream = mContext.openFileInput(fileName);int len = 0;byte[] buffer = new byte[1024];ByteArrayOutputStream byteArrayInputStream = new ByteArrayOutputStream();while ((len = fileInputStream.read(buffer)) != -1) {byteArrayInputStream.write(buffer, 0, len);}String string = new String(byteArrayInputStream.toByteArray());Log.d(TAG, string);} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (fileInputStream != null) {try {fileInputStream.close();} catch (IOException e) {e.printStackTrace();}}}}}

注意:使用寫入操作的時候,寫入的內容會將上次寫入的內容進行覆蓋

 

 

寫入的檔案儲存在/data/data/package name/files目錄下,使用DDMS可以進行查看

如所示:

使用DDMS將檔案匯出,即可查看內容

 

上面這些是將資料寫入到我們的手機內建的儲存空間裡,如果想寫入我們的SDCard,那麼應該怎麼做呢?

下面的寫入到SDCard的操作

 

 

// save infomation in the SDCardpublic boolean saveToSDCard(String fileName, String content) {// judge weather the SDCard exits,and can be read and writtenif (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {return false;}FileOutputStream fileOutputStream = null;File file = new File(Environment.getExternalStorageDirectory(),fileName);try {fileOutputStream = new FileOutputStream(file);fileOutputStream.write(content.getBytes());return true;} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {try {if (fileOutputStream != null) {fileOutputStream.close();}} catch (IOException e) {e.printStackTrace();}}return false;}

下面是讀取位於SDCard根目錄下檔案的操作方法

 

 

// read the file in the SDCardpublic String readFromSD(String fileName) {FileInputStream fileInputStream = null;File file = new File(Environment.getExternalStorageDirectory(),fileName);try {fileInputStream = new FileInputStream(file);int len = 0;byte[] buffer = new byte[1024];ByteArrayOutputStream byteArrayInputStream = new ByteArrayOutputStream();while ((len = fileInputStream.read(buffer)) != -1) {byteArrayInputStream.write(buffer, 0, len);}String string = new String(byteArrayInputStream.toByteArray());return string;} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} finally {if (fileInputStream != null) {try {fileInputStream.close();} catch (IOException e) {e.printStackTrace();}}}return null;}


 

 

 

聯繫我們

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