溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android 數(shù)據(jù)存儲(二) 文件的使用

發(fā)布時間:2020-06-04 21:24:00 來源:網(wǎng)絡(luò) 閱讀:446 作者:無用大叔 欄目:移動開發(fā)


    在Android中,我們可以將一些數(shù)據(jù)直接以文件的形式保存在設(shè)備中。例如:一些文本文件、PDF文件、音視頻文件和圖片等。Android  提供了文件讀寫的方法。


    通過 Context.openFileInput()方法獲得標(biāo)準Java文件輸入流(FileInputStream),通過Context.openFileOutput()方法獲得標(biāo)準Java文件輸出流( FileOutputStream )。使用

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


示例如下,新建一個Activity,添加兩個TextView和兩個Button,點擊第一個Button,將TextView上的數(shù)據(jù)寫到文件中,點擊第二個Button,將文件中的數(shù)據(jù)寫到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>


運行結(jié)果如下:        點擊write將第一個TextView寫入文件,點擊read將數(shù)據(jù)讀出到第二個TextView

                    Android 數(shù)據(jù)存儲(二)  文件的使用                                        Android 數(shù)據(jù)存儲(二)  文件的使用


向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI