标签:
目标效果:
程序运行,显示一输入框和按钮,在输入框输入内容点击按钮会提示保存成功,关闭程序,再次打开会在输入框显示刚才输入的内容,并提示英文。
1.activity_main.xml页面放置两个控件。
activity_main.xml页面:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <EditText android:id="@+id/etInput" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/btClick" android:layout_centerHorizontal="true" android:layout_marginBottom="34dp" android:ems="10" android:hint="请输入想保存的内容" > <requestFocus /> </EditText> <Button android:id="@+id/btClick" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:onClick="save" android:text="确定" /> </RelativeLayout>
2.新建FileService.java类,编写保存和读取方法。
FileService.java页面:
package com.example.file; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import android.content.Context; public class FileService { private Context context; public FileService(Context context){ this.context=context; } /*创建文件保存内容*/ public void save(String fileName,String content){ BufferedWriter bw=null; try { //打开文件,得到文件输出流(字节流),参数一为文件名,参数二为打开方式,为私有 FileOutputStream fos=context.openFileOutput(fileName,context.MODE_PRIVATE); //OutputStreamWriter把字节流转换成字符流,BufferedWriter创建缓冲区 bw=new BufferedWriter(new OutputStreamWriter(fos)); //往缓冲区写入内容 bw.write(content); } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); }finally{ try { if(bw!=null){ bw.close(); } } catch (IOException e) { e.printStackTrace(); } } } /*打开文件读取内容*/ public String read(String fileName){ String line; StringBuilder sb=new StringBuilder(); BufferedReader br=null; try { FileInputStream fis=context.openFileInput(fileName); br=new BufferedReader(new InputStreamReader(fis)); while((line=br.readLine())!=null){ sb.append(line); } } catch (FileNotFoundException e) { e.printStackTrace(); }catch (IOException e) { e.printStackTrace(); }finally{ try { if(br!=null){ br.close(); } } catch (IOException e) { e.printStackTrace(); } } return sb.toString(); } }
fileText.java页面:
package com.example.text; import com.example.file.FileService; import android.test.AndroidTestCase; import android.util.Log; public class fileText extends AndroidTestCase{ public void saveText(){ FileService fileservice=new FileService(getContext()); fileservice.save("out.txt","hello!"); } public void readText(){ FileService fileservice=new FileService(getContext()); String s=fileservice.read("out.txt"); Log.i("MainActivity","s="+s); } }
package com.example.file; import android.os.Bundle; import android.app.Activity; import android.view.View; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity { private EditText etInput; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /*获取控件方法*/ getId(); /*读取文件内容方法*/ read(); } /*读取文件内容方法*/ private void read() { FileService fileservice=new FileService(this); String content=fileservice.read("myfile.txt"); if(!content.isEmpty()){ etInput.setText(content); etInput.setSelection(content.length()); Toast.makeText(this,"Restoring succeeded",Toast.LENGTH_SHORT).show(); } } /*点击按钮调用save方法*/ public void save(View view){ FileService fileservice=new FileService(this); String content=etInput.getText().toString(); fileservice.save("myfile.txt",content); Toast.makeText(this,"保存成功",Toast.LENGTH_SHORT).show(); } /*获取控件方法*/ private void getId() { etInput=(EditText) findViewById(R.id.etInput); } }
8.按照以前博文介绍的方法,可以导保存到桌面查看文件中的内容,可以发现out.txt文件中是测试时保存的“hello!”,myfile.txt文件中是运行时输入的“123456”。
标签:
原文地址:http://blog.csdn.net/hester_hester/article/details/51335133