1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()...

android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()...

时间:2018-10-09 03:41:49

相关推荐

android文件的写入与读取---简单的文本读写context.openFileInput() context.openFileOutput()...

最终效果图,点击save会保存到文件中,点击show会从文件中读取出内容并显示。

main.xml

[xhtml]view plaincopy <?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:<ahref="/base/15"class='replace_word'title="Android知识库"target='_blank'style='color:#df3434;font-weight:bold;'>Android</a>="/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><TextViewandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:text="请您输入要保存的内容:"/><EditTextandroid:id="@+id/addText"android:layout_width="fill_parent"android:layout_height="wrap_content"android:hint="请您在此处输入文件内容!"/><Buttonandroid:id="@+id/addButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="save"/><Buttonandroid:id="@+id/showButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="show"/><TextViewandroid:id="@+id/showText"android:layout_width="fill_parent"android:layout_height="wrap_content"/></LinearLayout>

activity代码

1 package .filedome; 2 3 import android.content.Context; 4 import android.support.v7.app.AppCompatActivity; 5 import android.os.Bundle; 6 import android.util.Log; 7 import android.widget.EditText; 8 import android.widget.TextView; 9 10 import java.io.BufferedReader; 11 import java.io.BufferedWriter; 12 import java.io.ByteArrayOutputStream; 13 import java.io.FileInputStream; 14 import java.io.FileNotFoundException; 15 import java.io.FileOutputStream; 16 import java.io.IOException; 17 import java.io.InputStreamReader; 18 import java.io.OutputStreamWriter; 19 import java.io.FileInputStream; 20 import java.io.FileNotFoundException; 21 import java.io.FileOutputStream; 22 import java.io.IOException; 23 24 import android.app.Activity; 25 import android.os.Bundle; 26 import android.view.View; 27 import android.view.View.OnClickListener; 28 import android.widget.Button; 29 import android.widget.EditText; 30 import android.widget.TextView; 31 import android.widget.Toast; 32 33 public class MainActivity extends AppCompatActivity { 34 35private EditText editText; 36private TextView showTextView; 37// 要保存的文件名 38private String fileName = "chenzheng_java.txt"; 39 40@Override 41public void onCreate(Bundle savedInstanceState) { 42 super.onCreate(savedInstanceState); 43 setContentView(R.layout.activity_main); 44 // 获取页面中的组件 45 editText = (EditText) findViewById(R.id.addText); 46 showTextView = (TextView) findViewById(R.id.showText); 47 Button addButton = (Button) this.findViewById(R.id.addButton); 48 Button showButton = (Button) this.findViewById(R.id.showButton); 49 // 绑定单击事件 50 addButton.setOnClickListener(listener); 51 showButton.setOnClickListener(listener); 52 53} 54 55// 声明监听器 56private View.OnClickListener listener = new OnClickListener() { 57 public void onClick(View v) { 58 Button view = (Button) v; 59 switch (view.getId()) { 60 case R.id.addButton: 61 save(); 62 break; 63 case R.id.showButton: 64 read(); 65 break; 66 67 } 68 69 } 70 71}; 72 73/** 74* @author chenzheng_Java 75* 保存用户输入的内容到文件 76*/ 77private void save() { 78 BufferedWriter bufferdWriter = null; 79 String content = editText.getText().toString(); 80 try { 81 /* 根据用户提供的文件名,以及文件的应用模式,打开一个输出流.文件不存系统会为你创建一个的, 82 * 至于为什么这个地方还有FileNotFoundException抛出,我也比较纳闷。在Context中是这样定义的 83 * public abstract FileOutputStream openFileOutput(String name, int mode) 84 * throws FileNotFoundException; 85 * openFileOutput(String name, int mode); 86 * 第一个参数,代表文件名称,注意这里的文件名称不能包括任何的/或者/这种分隔符,只能是文件名 87 *该文件会被保存在/data/data/应用名称/files/chenzheng_java.txt 88 * 第二个参数,代表文件的操作模式 89 *MODE_PRIVATE 私有(只能创建它的应用访问) 重复写入时会文件覆盖 90 *MODE_APPEND 私有 重复写入时会在文件的末尾进行追加,而不是覆盖掉原来的文件 91 *MODE_WORLD_READABLE 公用 可读 92 *MODE_WORLD_WRITEABLE 公用 可读写 93 * */ 94 FileOutputStream outputStream = openFileOutput(fileName, 95 Activity.MODE_PRIVATE); 96 bufferdWriter = new BufferedWriter(new OutputStreamWriter(outputStream)); 97 bufferdWriter.write(content); 98 Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_LONG).show(); 99 } catch (Exception e) {100 e.printStackTrace();101 } finally {102 103 try {104 if (bufferdWriter != null) {105 bufferdWriter.close();106 }107 } catch (IOException e) {108 e.printStackTrace();109 }110 111 112 }113 114}115 116/**117* @author chenzheng_java118* 读取刚才用户保存的内容119*/120private void read() {121 122 BufferedReader bufferedReader = null;123 try {124 FileInputStream inputStream = this.openFileInput(fileName);125 126 bufferedReader = new BufferedReader(new InputStreamReader(inputStream));127 StringBuilder stirngBuidler = new StringBuilder();128 String line = "";129 while ((line = bufferedReader.readLine()) != null) {130 stirngBuidler.append(line);131 }132 133 showTextView.setText(stirngBuidler.toString());134 135 } catch (Exception e) {136 e.printStackTrace();137 } finally {138 139 try {140 if (bufferedReader != null) {141 bufferedReader.close();142 }143 } catch (IOException e) {144 e.printStackTrace();145 }146 147 148 }149 150}151 152 }

其他的都为默认。

关于文件保存的路径可以通过ADT携带的File Explorer工具进行查看。如何调出File Explorer工具呢;我们可以通过Windows--showView--others-android下面看到File Explorer。这里是我的一个截图。

对于这个程序,基本上没什么难点,就是纯粹的java流知识。唯一不同的就是context为我们提供了两个方法来获取输入输出流。简单、方便、快捷啊。

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。