Android 資料存放區(二) 檔案的使用

來源:互聯網
上載者:User

標籤:資料   檔案   android   


    在Android中,我們可以將一些資料直接以檔案的形式儲存在裝置中。例如:一些文字檔、PDF檔案、音視頻檔案和圖片等。Android  提供了檔案讀寫的方法。


    通過 Context.openFileInput()方法獲得標準Java檔案輸入資料流(FileInputStream),通過Context.openFileOutput()方法獲得標準Java檔案輸出資料流( FileOutputStream )。使用

Resources.openRawResource(R.raw.myDataFile)方法返回InputStream。


樣本如下,建立一個Activity,添加兩個TextView和兩個Button,點擊第一個Button,將TextView上的資料寫到檔案中,點擊第二個Button,將檔案中的資料寫到TextView中。


MainActivity.java:

public class MainActivity extends Activity {    private String filename = "file.txt";    private TextView mytext1,mytext2;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        mytext1=(TextView)this.findViewById(R.id.text1);        mytext2=(TextView)this.findViewById(R.id.text2);        Button button1=(Button)this.findViewById(R.id.btn_read);        Button button2=(Button)this.findViewById(R.id.btn_write);        button1.setOnClickListener(new OnClickListener(){            @Override            public void onClick(View arg0) {                // TODO Auto-generated method stub                mytext2.setText(read());            }        });        button2.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View arg0) {                // TODO Auto-generated method stub                write(mytext1.getText().toString());            }        });    }    protected String read(){        try {            FileInputStream fis = openFileInput(filename);            try {                byte[] buffer = new byte[fis.available()];                fis.read(buffer);                return new String(buffer);            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        } catch (FileNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }        return null;    }    protected void write(String str) {        try {            FileOutputStream fos = openFileOutput(filename,MODE_APPEND);            try {                fos.write(str.getBytes());                fos.close();            } catch (IOException e) {                // TODO Auto-generated catch block                e.printStackTrace();            }        } catch (FileNotFoundException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}


activity_main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical">        <TextView android:id="@+id/text1"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="我是text1"/>    <Button android:id="@+id/btn_write"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="write"/>    <TextView android:id="@+id/text2"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="我是text2"/>    <Button android:id="@+id/btn_read"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="read"/>    </LinearLayout>


運行結果如下:        點擊write將第一個TextView寫入檔案,點擊read將資料讀出到第二個TextView

                    650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/5A/D7/wKiom1T9pIajsG7cAADJWMCyuJo675.jpg" title="QQ20150309214359.png" alt="wKiom1T9pIajsG7cAADJWMCyuJo675.jpg" />                                        650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/5A/D2/wKioL1T9pavjCvM6AADC0C0wbRA526.jpg" title="2.png" alt="wKioL1T9pavjCvM6AADC0C0wbRA526.jpg" />


本文出自 “無用大叔” 部落格,請務必保留此出處http://aslonely.blog.51cto.com/6552465/1618819

Android 資料存放區(二) 檔案的使用

聯繫我們

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