如果说不想把内容存在SharedPreferences中的话,我们可以自己写一个文件保存须要的数据,在这里我将文件保存在系统中的工程路径下。
输入需要保存的内容

保存完毕后红框内呈现之前保存的数据

保存文件以后,文件被保存在了当前工程下 files 文件夹的路径下,这里说一下data文件夹 如果手机没有root 权限 用户是访问不到的,这种储存方式有一个麻烦的地方就是文件中保存的数据须要程序员自己去处理 , 好比文件中保存了很多字符串数据 但是我们只须要其中的一部分数据,这样就须要自己去写代码去从文件中拿需要的数据。
- public class FileActivity extends Activity {
- public final static String FILE_NAME = "a.txt";
- /**File中储存数据的路径**/
- public final static String DATA_URL = "/data/data/";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- setContentView(R.layout.file);
- /**读取内容**/
- String content = loadFile();
- if(content == null) {
- content ="上次没有输入内容请输入";
- }
- String str = "上次输入保存的内容的姓名为【 " +content + "】";
- final EditText editContent = ((EditText)findViewById(R.id.file_et0));
- editContent.setHint(str);
- Button button0 = (Button)findViewById(R.id.file_button0);
- /**监听按钮点击后保存用户输入信息到file中**/
- button0.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- /**拿到用户输入的信息**/
- String content = editContent.getText().toString();
- /**开始保存入file**/
- saveFile(content);
- ShowDialog("保存File文件成功");
- }
- });
- Button button1 = (Button)findViewById(R.id.file_button1);
- /**监听按钮点击后清空file中内容**/
- button1.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- cleanFile();
- ShowDialog("清空File文件成功");
- }
- });
- Button button2 = (Button)findViewById(R.id.file_button2);
- /**监听按钮点击后删除file文件**/
- button2.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View arg0) {
- File file = new File(DATA_URL + getPackageName().toString()
- + "/files", FILE_NAME);
- if (file.exists()) {
- file.delete();
- }
- ShowDialog("删除file文件成功");
- }
- });
- super.onCreate(savedInstanceState);
- }
- /**
- * 保存内容
- * @param str
- */
- public void saveFile(String str) {
- try {
- FileOutputStream outStream = this.openFileOutput(FILE_NAME,
- Context.MODE_WORLD_READABLE);
- outStream.write(str.getBytes());
- outStream.close();
- } catch (FileNotFoundException e) {
- } catch (IOException e) {
- }
- }
- /**
- * 因为java删除文件内容只有一种实现方法,就是把整个文件重写,只是把须要删除的那一条记录去除掉
- */
- public void cleanFile() {
- //如果只须要删除文件中的一部分内容则须要在这里对字符串做一些操作
- String cleanStr = "";
- try {
- FileOutputStream outStream = this.openFileOutput(FILE_NAME,
- Context.MODE_WORLD_READABLE);
- outStream.write(cleanStr.getBytes());
- outStream.close();
- } catch (FileNotFoundException e) {
- } catch (IOException e) {
- }
- }
- public String loadFile() {
- try {
- FileInputStream inStream = this.openFileInput(FILE_NAME);
- ByteArrayOutputStream stream = new ByteArrayOutputStream();
- byte[] buffer = new byte[1024];
- int length = -1;
- while ((length = inStream.read(buffer)) != -1) {
- stream.write(buffer, 0, length);
- }
- stream.close();
- inStream.close();
- return stream.toString();
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } catch (IOException e) {
- }
- return null;
- }
- public void ShowDialog(String str) {
- AlertDialog.Builder builder = new AlertDialog.Builder(FileActivity.this);
- builder.setIcon(R.drawable.icon);
- builder.setTitle(str);
- builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int whichButton) {
- finish();
- }
- });
- builder.show();
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:Android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical"
- >
- <ImageView android:id="@+id/file_image"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/jay"
- android:layout_gravity="center"
- />
- <EditText android:id="@+id/file_et0"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:hint="请输入需要保存的内容">
- </EditText>
- <Button android:id="@+id/file_button0"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="保存入file">
- </Button>
- <Button android:id="@+id/file_button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="清除file保存内容">
- </Button>
- <Button android:id="@+id/file_button2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="删除file文件">
- </Button>
- </LinearLayout>
