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);}}

更多相关文章

  1. Android实现文件上传功能(接收端用strust2)
  2. android 获取文件的扩展名和去掉文件的扩展名
  3. android平台上的文件下载,文件和文件的操作
  4. Android APK文件安装过程小结
  5. Android 文件操作
  6. android从资源文件中读取文件流显示
  7. android 快速移动sdCard的文件夹
  8. Android Studio module里面放switch语句报错 R文件找不到

随机推荐

  1. Android(安卓)RecyclerView多布局的实现
  2. Android(安卓)混合开发环境搭建
  3. Android(安卓)QQ和微信分享
  4. Android(安卓)多点触控抬起手指,onTouchEv
  5. Android(安卓)PathMeasure的使用详解
  6. Android(安卓)第三方类库简单使用之Event
  7. android studio 绘制时钟刻度表盘的虚拟
  8. Android(安卓)应用换肤功能(白天黑夜主题
  9. Android(安卓)6.0中添加硬件抽象层(HAL)
  10. QTrace:一个不一样的Android(安卓)IDE