读写内部存储的文件数据

    把  XXX.txt文件 写入/读取  在data/data/包  目录下面。

 

 1 /** 读写内部存储的文件数据 */
 2         findViewById(R.id.btnWrite).setOnClickListener(
 3                 new View.OnClickListener() {
 4                     @Override
 5                     public void onClick(View v) {
 6                         try {
 7                             FileOutputStream fas = openFileOutput("text",
 8                                     Context.MODE_PRIVATE);
 9                             OutputStreamWriter asw = new OutputStreamWriter(
10                                     fas, "UTF-8");
11                             asw.write(et.getText().toString());
12                             asw.flush();
13                             fas.flush();
14                             asw.close();
15                             fas.close();
16                             Toast.makeText(getApplicationContext(), "写入完成。",
17                                     Toast.LENGTH_SHORT).show();
18                         } catch (FileNotFoundException e) {
19                             e.printStackTrace();
20                         } catch (UnsupportedEncodingException e) {
21                             e.printStackTrace();
22                         }
23                     }
24                 });
25         findViewById(R.id.btnRead).setOnClickListener(
26                 new View.OnClickListener() {
27                     @Override
28                     public void onClick(View v) {
29                         try {
30                             FileInputStream fis = openFileInput("text");
31                             InputStreamReader is = new InputStreamReader(fis,
32                                     "UTF-8");
33                             char input[] = new char[fis.available()];
34                             is.read(input);
35                             is.close();
36                             fis.close();
37                             String readed = new String(input);
38                             show.setText(readed);
39                         } catch (FileNotFoundException e) {
40                             e.printStackTrace();
41                         } catch (IOException e) {
42                             e.printStackTrace();
43                         }
44                     }
45                 });

 

转载于:https://www.cnblogs.com/androidsj/p/3910418.html

你可能感兴趣的:(读写内部存储的文件数据)