文件内部写入及读取(参考疯狂安卓讲义)
参考了参考疯狂安卓讲义的内部读取,这个是标准版答案,做下备份,开始基于这个的改进:
建的工程名为FileTest,包名为com.swust.filetest
activity_main.xml代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <EditText android:id="@+id/edit1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:lines="4"/> <Button android:id="@+id/write" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/write"/> <EditText android:id="@+id/edit2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:editable="false" android:cursorVisible="false" android:lines="4"/> <Button android:id="@+id/read" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/read"/> </LinearLayout>
用到了strings.xml:
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, FileTest!</string> <string name="app_name">文件读写入门</string> <string name="read">读取</string> <string name="write">写入</string> <string name="action_settings">hello</string> </resources>
MainActivity.java代码:
package com.swust.filetest; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.PrintStream; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { final String FILE_NAME = "1.txt"; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); System.out.println(new StringBuilder("a").append("b").append("c") .toString()); // 获取两个按钮 Button read = (Button) findViewById(R.id.read); Button write = (Button) findViewById(R.id.write); // 获取两个文本框 final EditText edit1 = (EditText) findViewById(R.id.edit1); final EditText edit2 = (EditText) findViewById(R.id.edit2); // 为write按钮绑定事件监听器 write.setOnClickListener(new OnClickListener() { @Override public void onClick(View source) { // 将edit1中的内容写入文件中 write(edit1.getText().toString()); edit1.setText(""); } }); read.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // 读取指定文件中的内容,并显示出来 edit2.setText(read()); } }); } private String read() { try { // 打开文件输入流 FileInputStream fis = openFileInput(FILE_NAME); byte[] buff = new byte[1024]; int hasRead = 0; StringBuilder sb = new StringBuilder(""); // 读取文件内容 while ((hasRead = fis.read(buff)) > 0) { sb.append(new String(buff, 0, hasRead)); } // 关闭文件输入流 fis.close(); return sb.toString(); } catch (Exception e) { e.printStackTrace(); } return null; } private void write(String content) { try { // 以追加模式打开文件输出流MODE_APPEND //该文件只能被当前程序读写,也就是每次重新写模式 FileOutputStream fos = openFileOutput(FILE_NAME,MODE_PRIVATE ); // 将FileOutputStream包装成PrintStream PrintStream ps = new PrintStream(fos); // 输出文件内容 ps.println(content); // 关闭文件输出流 ps.close(); } catch (Exception e) { e.printStackTrace(); } } }