温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android 数据存储(二) 文件的使用

发布时间:2020-06-04 21:24:00 来源:网络 阅读:442 作者:无用大叔 栏目:移动开发


    在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

                    Android 数据存储(二)  文件的使用                                        Android 数据存储(二)  文件的使用


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI