Android儲存和載入本地檔案(內部存放裝置),android存放裝置

來源:互聯網
上載者:User

Android儲存和載入本地檔案(內部存放裝置),android存放裝置

Android裝置上的所有應用都有一個放置在沙盤中的檔案目錄,將檔案儲存到沙箱中可以阻止其他應用的訪問。

沙箱目錄的全路徑為:/data/data/<包名>  用File Explorer查看:


如可見,每個應用都在/data/data下有一個以此應用程式套件名命名的檔案目錄。

而本文就是介紹將檔案儲存在/data/data/<包名>/files/ 目錄下


下面就展示如何在內部存放裝置中儲存和載入本地檔案:


1、建立一個名為 DataStorage的工程


2、準備好布局檔案(activity_data_storage.xml)

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:orientation="vertical" >    <TextView        android:id="@+id/data_view"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="未操作" />    <Button        android:id="@+id/save_button"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="儲存資料" />    <Button        android:id="@+id/load_button"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="載入資料" /> </LinearLayout>


3、DataStorageActivity.java

package com.example.datastorage;import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.io.OutputStream;import java.io.OutputStreamWriter;import java.io.Writer;import android.content.Context;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.TextView;import android.widget.Toast;public class DataStorageActivity extends ActionBarActivity {private static final String FILENAME = "data.txt";private static final String TAG = "DataStorageActivity";private TextView dataView;private Button saveButton;private Button loadButton;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_date_storage);dataView = (TextView) findViewById(R.id.data_view);saveButton = (Button) findViewById(R.id.save_button);loadButton = (Button) findViewById(R.id.load_button);setListener();}private void setListener() {saveButton.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {try {saveData();} catch (IOException e) {}Toast.makeText(DataStorageActivity.this, "儲存成功",Toast.LENGTH_SHORT).show();}});loadButton.setOnClickListener(new View.OnClickListener() {public void onClick(View v) {try {loadData();} catch (FileNotFoundException e) {} catch (IOException e) {}}});}public void saveData() throws IOException {OutputStream out = this.openFileOutput(FILENAME, Context.MODE_PRIVATE);/* 參數一: 檔案名稱。 * 如果檔案不存在,Android會自動建立它。建立的檔案儲存在/data/data/<package name>/files目錄下 * 參數二: 檔案操作模式參數。代表該檔案是私人資料,只能被應用本身訪問。 * */Writer writer = new OutputStreamWriter(out);try {String str = "來自儲存在內部存放裝置的資料";writer.write(str);} finally {writer.close();}}public void loadData() throws FileNotFoundException, IOException {BufferedReader reader = null;StringBuilder data = new StringBuilder();try {InputStream in = this.openFileInput(FILENAME);Log.i(TAG, in.toString());reader = new BufferedReader(new InputStreamReader(in));String line = new String();while ((line = reader.readLine()) != null) {data.append(line);}dataView.setText(data);} catch (FileNotFoundException e) {dataView.setText("沒有發現儲存的資料");} finally {reader.close();}}}

運行程式,點擊儲存資料按鈕,Toast顯示儲存成功字樣後。再點擊載入資料按鈕:



可以發現儲存在內部存放裝置的裝置被載入後在TextView顯示。再看檔案具體位置:





#DONE#



Android的“檔案管理工具”,是怎獲得“本機存放區”與“外置儲存”路徑的?

外部儲存路徑(即sdcard):Environment.getExternalStorageDirectory().getPath();

可以列出所有的目錄:

Environment.getRootDirectory().getParent()
 
安卓手機的“內部儲存空間”“內建儲存卡”“外置儲存卡”分別是什?

內部儲存空間其實叫RAM是手機運行記憶體 這個越大越好 非常重要
內建儲存卡就是手機內建記憶體好像又叫什麼rom
外置儲存卡是SD卡用來儲存檔案 資料
 

聯繫我們

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