内部文件存储
內部文件存儲 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 那消逝的歲月 閱讀(...) 評論(...) 編輯 收藏轉載于:https://www.cnblogs.com/future-zhenzhen/p/5533475.html
總結
- 上一篇: ssh密钥对验证
- 下一篇: 如何通过插件携带第二个单据体到下游单据