首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

Android 资料的保存和读取

2012-08-30 
Android 文件的保存和读取Android 给我们提供了两个方法返回输入、输出流,分别为:openFileInput(String fil

Android 文件的保存和读取
Android 给我们提供了两个方法返回输入、输出流,分别为:openFileInput(String fileName)
、openFileOutput(String fileName,int mode);
下面看一自己写了一个简单的例子:

<?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" >    <TextView        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:text="@string/hello" />    <EditText         android:id="@+id/fileName"        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="请输入文件名称"        >       <requestFocus />    </EditText>    <EditText        android:id="@+id/fileContent"        android:layout_width="match_parent"        android:layout_height="200dp"        />        <LinearLayout          android:orientation="horizontal"                 android:layout_width="wrap_content"                 android:layout_height="wrap_content"        >        <Button              android:id="@+id/saveData"             android:text="保存"             android:layout_width="wrap_content"             android:layout_height="wrap_content"            />        <Button              android:id="@+id/readData"             android:text="读取"             android:layout_width="wrap_content"             android:layout_height="wrap_content"            />    </LinearLayout></LinearLayout>


public class FileActivity extends Activity {    EditText fileName;    EditText fileContent;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                fileName = (EditText)findViewById(R.id.fileName);        fileContent = (EditText)findViewById(R.id.fileContent);                Button saveData = (Button)findViewById(R.id.saveData);        saveData.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {OutputStream output;try {      output =                               FileActivity.this.openFileOutput(fileName.getText().toString(),                                                                               Context.MODE_APPEND);FileService.save(output, fileContent.getText().toString());Toast.makeText(getApplicationContext(), "保存成功",                                                        Toast.LENGTH_LONG).show();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}});                Button readData  = (Button)findViewById(R.id.readData);        readData.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {try {InputStream intput =                               FileActivity.this.openFileInput(fileName.getText().toString());      String content = FileService.read(intput);Toast.makeText(getApplicationContext(), content,                                                      Toast.LENGTH_LONG).show();} catch (FileNotFoundException e) {// TODO Auto-generated catch blocke.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}});    }}

FileService.java
public class FileService {/** * 保存文件 * @param output * @param content * @throws IOException */public static void save(OutputStream output,String content) throws IOException{output.write(content.getBytes());output.close();}/** * 读取文件 * @param input * @throws IOException  */public static String read(InputStream intput) throws IOException{ByteArrayOutputStream output = new ByteArrayOutputStream();byte[] bt = new byte[1024];int length = intput.read(bt);while(length != -1){output.write(bt, 0, bt.length);length = intput.read(bt);}byte[] data = output.toByteArray();intput.close();output.close();return new String(data);}}

热点排行