Android开发中会用到文件存储,今天来学习下。
先改下布局界面:
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" > 6 7 <TextView 8 android:id="@+id/filename" 9 android:layout_width="wrap_content" 10 android:layout_height="20dp" 11 android:textSize="15dp" 12 android:text="文件名称" /> 13 14 <EditText 15 android:id="@+id/edit_name" 16 android:layout_width="match_parent" 17 android:layout_height="wrap_content"/> 18 19 <TextView 20 android:id="@+id/filecontent" 21 android:layout_width="wrap_content" 22 android:layout_height="20dp" 23 android:textSize="15dp" 24 android:text="文件内容" /> 25 26 <EditText 27 android:id="@+id/edit_content" 28 android:layout_width="match_parent" 29 android:layout_height="wrap_content"/> 30 31 <LinearLayout 32 android:layout_width="match_parent" 33 android:layout_height="match_parent" 34 android:orientation="horizontal" > 35 36 <Button 37 android:id="@+id/btn_save" 38 android:layout_width="wrap_content" 39 android:layout_height="wrap_content" 40 android:text="保存文件" /> 41 42 <Button 43 android:id="@+id/btn_read" 44 android:layout_width="wrap_content" 45 android:layout_height="wrap_content" 46 android:text="读取文件" /> 47 </LinearLayout> 48 49 </LinearLayout>
再改下MainActivity文件:
1 package com.example.filesave; 2 3 import java.io.ByteArrayOutputStream; 4 import java.io.FileInputStream; 5 import java.io.FileOutputStream; 6 7 import android.app.Activity; 8 import android.content.Context; 9 import android.os.Bundle; 10 import android.view.View; 11 import android.widget.Button; 12 import android.widget.EditText; 13 14 public class MainActivity extends Activity { 15 16 private Context context = this; 17 private Button btnsave = null; 18 private Button btnread = null; 19 private EditText editname = null; 20 private EditText editcontent = null; 21 22 @Override 23 protected void onCreate(Bundle savedInstanceState) { 24 super.onCreate(savedInstanceState); 25 setContentView(R.layout.activity_main); 26 27 btnsave = (Button) findViewById(R.id.btn_save); 28 btnread = (Button) findViewById(R.id.btn_read); 29 editname = (EditText) findViewById(R.id.edit_name); 30 editcontent = (EditText) findViewById(R.id.edit_content); 31 32 btnsave.setOnClickListener(new View.OnClickListener() { 33 34 @Override 35 public void onClick(View v) { 36 37 String filename = editname.getText().toString(); 38 String filecontent = editcontent.getText().toString(); 39 FileOutputStream out = null; 40 try { 41 out = context 42 .openFileOutput(filename, Context.MODE_PRIVATE); 43 out.write(filecontent.getBytes("UTF-8")); 44 45 editname.setText("已经保存名称!"); 46 editcontent.setText("已经保存内容!"); 47 48 } catch (Exception e) { 49 e.printStackTrace(); 50 } finally { 51 try { 52 out.close(); 53 } catch (Exception e) { 54 e.printStackTrace(); 55 } 56 } 57 } 58 }); 59 60 btnread.setOnClickListener(new View.OnClickListener() { 61 62 @Override 63 public void onClick(View v) { 64 String filename = editname.getText().toString(); // 获得读取的文件的名称 65 FileInputStream in = null; 66 ByteArrayOutputStream bout = null; 67 byte[] buf = new byte[1024]; 68 bout = new ByteArrayOutputStream(); 69 int length = 0; 70 try { 71 in = context.openFileInput(filename); // 获得输入流 72 while ((length = in.read(buf)) != -1) { 73 bout.write(buf, 0, length); 74 } 75 byte[] content = bout.toByteArray(); 76 editcontent.setText(new String(content, "UTF-8")); // 设置文本框为读取的内容 77 } catch (Exception e) { 78 e.printStackTrace(); 79 } 80 editcontent.invalidate(); // 刷新屏幕 81 try { 82 in.close(); 83 bout.close(); 84 } catch (Exception e) { 85 } 86 } 87 88 89 }); 90 91 } 92 93 }
运行效果:
分别输入文件名称和文件内容为“aaaa"、”1234“,如图:
点击保存文件后,界面变为:
然后再修改文件名称为”aaaa",如图:
最后点击读取文件按钮,文件内容显示为”1234”,测试成功。如图:
时间: 2024-10-14 06:27:42