频道栏目
首页 > 资讯 > 其他 > 正文

利用File来储存和读取

17-05-03        来源:[db:作者]  
收藏   我要投稿

利用File来储存和读取

1.MainActivity.java

public class MainActivity extends AppCompatActivity {
    final String FILE_NAME = "crazyit.bin";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        System.out.println(new StringBuilder("a").append("b").append("c").toString());
        Button read = (Button) findViewById(R.id.read);
        Button write = (Button) findViewById(R.id.write);

        final EditText edit1 = (EditText) findViewById(R.id.edit1);
        final EditText edit2 = (EditText) findViewById(R.id.edit2);

        write.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //将edit1的内容写入文件中
                write(edit1.getText().toString());
                edit1.setText("");
            }
        });

        read.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                edit2.setText(read());
            }
        });
    }

    private String read() {
        try {
            //1.打开文件的输入流
            FileInputStream fis = openFileInput(FILE_NAME);
            // 2.定义存储空间
            byte[] buff = new byte[1024];
            int hasRead = 0;
            //  StringBuilder不支持并发操作,线性不安全的,不适合多线程中使用
            StringBuilder sb = new StringBuilder("");
            // 3.开始读文件,记取文件 的内容
            while ((hasRead = fis.read(buff)) > 0) {
                sb.append(new String(buff, 0, hasRead));
            }
            // 4.关闭文件输入流
            fis.close();
            return sb.toString();

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    private void write(String content) {
        try {
            //以追加的方式打开文件输出流
            FileOutputStream fos = openFileOutput(FILE_NAME, MODE_APPEND);
//         将FileOutputStream 包装成printStream
            PrintStream ps = new PrintStream(fos);
            ps.println(content);
            ps.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

2.xml文件

activity_main.xml

xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:id="@+id/activity_main"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.sh.appopenfileinputoutput.MainActivity">

<EditText
    android:id="@+id/edit1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/read"
        android:text="read "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <EditText
        android:id="@+id/edit2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    <Button
        android:id="@+id/write"
        android:text="write "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

LinearLayout>
相关TAG标签
上一篇:多人聊天(无问题的)
下一篇:Android studio 2.2 新建虚拟机
相关文章
图文推荐

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站