读写内部存储的文件数据



创建一个工程LearnReadWriteInternalData,空Activity 及一个按钮,

先直接运行到手机或虚拟机中,此时就会在手机系统的固定地方创建一个文件夹

Android系统的Data文件夹\内部的Data\找到我们的工程

 

Andriod Studio的工具栏中选择DDMSAndroidDevice Monitor

<EditText

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:id="@+id/et"

    android:hint="在这里输入内容" />

  

<Button

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="保存数据"

    android:id="@+id/btnWrite" />

  

<Button

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="读取数据"

    android:id="@+id/btnRead" />

  

<TextView

    android:layout_width="fill_parent"

    android:layout_height="wrap_content"

    android:text="New Text"

    android:id="@+id/tvShow" />

 

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView show;

    private EditText et;

    private String fileName="test";

  

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        show= (TextView) findViewById(R.id.tvShow);

        et= (EditText) findViewById(R.id.et);

        findViewById(R.id.btnRead).setOnClickListener(this);

        findViewById(R.id.btnWrite).setOnClickListener(this);

    }

  

    @Override

    public void onClick(View v) {

        switch (v.getId()){

            case R.id.btnRead:

                try {

                    FileInputStream fis= openFileInput(fileName);

                    InputStreamReader is=new InputStreamReader(fis,"UTF-8");

                    char input[]=new char[fis.available()];

                    is.read(input);

                    is.close();

                    fis.close();

                    String readed=new String(input);

                    show.setText(readed);

                    System.out.println("读取数据成功");

                } catch (FileNotFoundException e) {

                    e.printStackTrace();

                } catch (UnsupportedEncodingException e) {

                    e.printStackTrace();

                } catch (IOException e) {

                    e.printStackTrace();

                }

                break;

            case R.id.btnWrite:

                try {

                    FileOutputStream fos=openFileOutput(fileName, Context.MODE_PRIVATE);  //应用程序输入写入内部存储

                    OutputStreamWriter osw=new OutputStreamWriter(fos,"UTF-8");

                    osw.write(et.getText().toString());

                    osw.flush();

                    fos.flush();

                    osw.close();

                    fos.close();

                    Toast.makeText(MainActivity.this, "写入内部数据完成", Toast.LENGTH_LONG);

                    System.out.println("写入内部数据完成");

  

                } catch (FileNotFoundException e) {

                    e.printStackTrace();

                } catch (UnsupportedEncodingException e) {

                    e.printStackTrace();

                } catch (IOException e) {

                    e.printStackTrace();

                }

                break;

        }

    }

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

牵手生活

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值