
Android开发中会用到文件存储,今天来学习下。
先改下布局界面:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" > <TextView
android:id="@+id/filename"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:textSize="15dp"
android:text="文件名称" /> <EditText
android:id="@+id/edit_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"/> <TextView
android:id="@+id/filecontent"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:textSize="15dp"
android:text="文件内容" /> <EditText
android:id="@+id/edit_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"/> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" > <Button
android:id="@+id/btn_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="保存文件" /> <Button
android:id="@+id/btn_read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="读取文件" />
</LinearLayout> </LinearLayout>
再改下MainActivity文件:
package com.example.filesave; import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream; import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText; public class MainActivity extends Activity { private Context context = this;
private Button btnsave = null;
private Button btnread = null;
private EditText editname = null;
private EditText editcontent = null; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); btnsave = (Button) findViewById(R.id.btn_save);
btnread = (Button) findViewById(R.id.btn_read);
editname = (EditText) findViewById(R.id.edit_name);
editcontent = (EditText) findViewById(R.id.edit_content); btnsave.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) { String filename = editname.getText().toString();
String filecontent = editcontent.getText().toString();
FileOutputStream out = null;
try {
out = context
.openFileOutput(filename, Context.MODE_PRIVATE);
out.write(filecontent.getBytes("UTF-8")); editname.setText("已经保存名称!");
editcontent.setText("已经保存内容!"); } catch (Exception e) {
e.printStackTrace();
} finally {
try {
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}); btnread.setOnClickListener(new View.OnClickListener() { @Override
public void onClick(View v) {
String filename = editname.getText().toString(); // 获得读取的文件的名称
FileInputStream in = null;
ByteArrayOutputStream bout = null;
byte[] buf = new byte[1024];
bout = new ByteArrayOutputStream();
int length = 0;
try {
in = context.openFileInput(filename); // 获得输入流
while ((length = in.read(buf)) != -1) {
bout.write(buf, 0, length);
}
byte[] content = bout.toByteArray();
editcontent.setText(new String(content, "UTF-8")); // 设置文本框为读取的内容
} catch (Exception e) {
e.printStackTrace();
}
editcontent.invalidate(); // 刷新屏幕
try {
in.close();
bout.close();
} catch (Exception e) {
}
} }); } }
运行效果:
分别输入文件名称和文件内容为“aaaa"、”1234“,如图:
点击保存文件后,界面变为:
然后再修改文件名称为”aaaa",如图:
最后点击读取文件按钮,文件内容显示为”1234”,测试成功。如图:
提示:创建的存储文件保存在/data/data/<package name>/files文件夹下,那么我们怎样才能看到这文件呢?首先连接手机处于调试模式,进入adb shell命令行界面, 输入“su”命令回车,如图
然后再输入:cd data/data 如图:
再输入ls 命令,找到自己创建工程包名文件就可以了,比如我的工程是“com.example.filesave”,那就cd com.example.filesave即可,如图:
最后找到files就可看到文件了。