android資料存放區之File

來源:互聯網
上載者:User

標籤:

android中使用File進行儲存主要使用到OpenFileOutput和OpenFileInput兩個方法,下面直接用一個例子來說明一下。

(1)布局檔案main.xml檔案

<?xml version="1.0" encoding="utf-8"?><TableLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >        <TableRow >        <TextView             android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="檔案名稱:"            android:textSize="20sp"/>        <EditText             android:id="@+id/name"            android:layout_width="200dp"            android:layout_height="wrap_content"            android:text=""            />    </TableRow>     <TableRow >        <TextView             android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="內容:"            android:textSize="20sp"/>        <EditText             android:id="@+id/content"            android:layout_width="200dp"            android:layout_height="100dp"            android:text=""            />    </TableRow>         <Button          android:id="@+id/save"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="儲存"         android:textSize="20sp"         />     <Button          android:id="@+id/read"         android:layout_width="fill_parent"         android:layout_height="wrap_content"         android:text="讀取"         android:textSize="20sp"         />     <!-- 用於顯示儲存在File中的資料 -->    <TextView         android:id="@+id/result"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text=""        android:textSize="20sp"/></TableLayout>

(2)service方法

package com.yby.file;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;public class FileService {    //檔案的儲存    public void save(OutputStream outstream,String content) throws IOException{        outstream.write(content.getBytes());        outstream.close();    }        //檔案的讀取    public String read(InputStream inputstream) throws IOException{        ByteArrayOutputStream baos = new ByteArrayOutputStream();        byte[] buffer = new byte[1024];        int len ;        while((len=inputstream.read(buffer))!=-1){            baos.write(buffer,0,len);        }        byte[] data = baos.toByteArray();        baos.close();        inputstream.close();        return new String(data);    }}

(3)activity代碼實現

package com.yby.file;import java.io.FileNotFoundException;import java.io.InputStream;import java.io.OutputStream;import android.app.Activity;import android.content.Context;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;public class MainActivity  extends Activity{    private EditText name,content;    private Button save,read;    private TextView result;    @Override    protected void onCreate(Bundle savedInstanceState) {        // TODO Auto-generated method stub        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        name = (EditText) findViewById(R.id.name);        content = (EditText) findViewById(R.id.content);        save = (Button) findViewById(R.id.save);        read = (Button) findViewById(R.id.read);        result = (TextView) findViewById(R.id.result);        save.setOnClickListener(listener);        read.setOnClickListener(listener);    }            private OnClickListener listener = new OnClickListener() {                @Override        public void onClick(View v) {            // TODO Auto-generated method stub            switch (v.getId()) {            case R.id.save:                try {                    // Context.MODE_PRIVATE:為預設操作模式,代表該檔案是私人資料,只能被應用本身訪問,在該模式下,寫入的內容會覆蓋原檔案的內容,如果想把新寫入的內容追加到原檔案中。可以使用Context.MODE_APPEND                    // Context.MODE_APPEND:模式會檢查檔案是否存在,存在就往檔案追加內容,否則就建立新檔案。                    // Context.MODE_WORLD_READABLE和Context.MODE_WORLD_WRITEABLE用來控制其他應用是否有許可權讀寫該檔案。                    // MODE_WORLD_READABLE:表示當前檔案可以被其他應用讀取;MODE_WORLD_WRITEABLE:表示當前檔案可以被其他應用寫入。                    // 如果希望檔案被其他應用讀和寫,可以傳入:                    // openFileOutput("output.txt", Context.MODE_WORLD_READABLE + Context.MODE_WORLD_WRITEABLE);                    OutputStream outstream =  MainActivity.this.openFileOutput(name.getText().toString(), Context.MODE_APPEND);                    FileService service = new FileService();                    service.save(outstream, content.getText().toString());                    Toast.makeText(MainActivity.this, "儲存成功", Toast.LENGTH_SHORT).show();                } catch (Exception e) {                    e.printStackTrace();                }                break;            case R.id.read:                try {                    InputStream instream =  MainActivity.this.openFileInput(name.getText().toString());                    FileService service = new FileService();                    String str = service.read(instream);                    result.setText(str);                } catch (Exception e) {                    e.printStackTrace();                }                break;            default:                break;            }        }    };}

(4)使用shell命令查看寫入的檔案內容

android資料存放區之File

聯繫我們

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