当前位置: 代码迷 >> Android >> Android 资料的保存和读取
  详细解决方案

Android 资料的保存和读取

热度:25   发布时间:2016-05-01 17:00:34.0
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() {			@Override			public 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 block					e.printStackTrace();				} catch (IOException e) {					// TODO Auto-generated catch block					e.printStackTrace();				}			}		});                Button readData  = (Button)findViewById(R.id.readData);        readData.setOnClickListener(new OnClickListener() {			@Override			public 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 block					e.printStackTrace();				} catch (IOException e) {					// TODO Auto-generated catch block					e.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);	}}

  相关解决方案