那消逝的岁月

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理
 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     xmlns:tools="http://schemas.android.com/tools"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="com.hanqi.testapp3.MainActivity"
11     android:orientation="vertical">
12 
13     <TextView
14         android:layout_width="wrap_content"
15         android:layout_height="wrap_content"
16         android:text="Hello World!"
17         android:id="@+id/tv_1"/>
18 
19     <EditText
20         android:layout_width="match_parent"
21         android:layout_height="wrap_content"
22         android:hint="输入..."
23         android:id="@+id/et_1"/>
24 
25     <Button
26         android:layout_width="match_parent"
27         android:layout_height="wrap_content"
28         android:text="写内部文件"
29         android:onClick="bt2_OnClick"/>
30 
31     <Button
32         android:layout_width="match_parent"
33         android:layout_height="wrap_content"
34         android:text="读内部文件"
35         android:onClick="bt3_OnClick"/>
36 </LinearLayout>
xml
 1 package com.hanqi.testapp3;
 2 
 3 import android.content.SharedPreferences;
 4 import android.os.Bundle;
 5 import android.support.v7.app.AppCompatActivity;
 6 import android.view.View;
 7 import android.widget.EditText;
 8 import android.widget.TextView;
 9 import android.widget.Toast;
10 
11 import java.io.File;
12 import java.io.FileInputStream;
13 import java.io.FileOutputStream;
14 import java.io.PrintStream;
15 
16 public class MainActivity extends AppCompatActivity {
17 
18     EditText et_1;
19     TextView tv_1;
20 
21     @Override
22     protected void onCreate(Bundle savedInstanceState) {
23         super.onCreate(savedInstanceState);
24         setContentView(R.layout.activity_main);
25 
26         et_1 = (EditText)findViewById(R.id.et_1);
27         tv_1 = (TextView)findViewById(R.id.tv_1);
28 
29     }
30 
31 //写内部文件
32     public void bt2_OnClick(View v)
33     {
34         //从内存里写入文件
35 
36         //1、得到内部的存储目录
37         try {
38 
39         File file = getFilesDir();
40 
41         String path = file.getAbsolutePath();
42 
43         Toast.makeText(MainActivity.this, "path = " + path, Toast.LENGTH_SHORT).show();
44 
45         //2、用输出流写入文件
46         FileOutputStream fos = openFileOutput("text.txt",MODE_APPEND);
47 
48         //3、写入文件内容
49             PrintStream ps = new PrintStream(fos);
50 
51             String str = et_1.getText().toString();
52 
53             ps.println(str);
54             //ps.println("自动换行");
55             ps.close();
56             fos.close();
57 
58             Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
59         }
60         catch (Exception e)
61         {
62             Toast.makeText(MainActivity.this, "保存失败", Toast.LENGTH_SHORT).show();
63         }
64 
65     }
66 
67 
68     //
69     public void bt3_OnClick (View v)
70     {
71         try {
72             //输入流
73             FileInputStream fis = openFileInput("text.txt");
74 
75             //1、定义byte[]
76             byte[] b = new byte[1024];
77             int i = 0; //读到的数据长度
78 
79             String str1 = "";
80 
81             //2、循环读
82             while ((i = fis.read(b)) > 0)
83             {
84                 String str = new String(b, 0, i);
85 
86                 str1 += str;
87             }
88 
89             fis.close();
90 
91             tv_1.setText(str1);
92         }
93         catch (Exception ex)
94         {
95 
96         }
97     }
98 
99 }
Java

 

posted on 2016-05-27 09:30  那消逝的岁月  阅读(139)  评论(0编辑  收藏  举报