Android编程之文件的读写实例详解

2019-12-10 19:43:38王旭
易采站长站为您分析Android编程之文件的读写方法,结合实例形式较为详细的分析了Android针对文件操作的详细步骤,常用函数及使用技巧,需要的朋友可以参考下  

本文实例分析了Android编程之文件的读写方法。,具体如下:

Android的文件读写与JavaSE的文件读写相同,都是使用IO流。而且Android使用的正是JavaSE的IO流,下面我们通过一个练习来学习Android的文件读写。

1.创建一个Android工程

Project name:File 
    BuildTarget:Android2.2 
    Application name:文件读写 
    Package name:test.file 
    Create Activity:DateActivity 
    Min SDK Version:8

strings.xml文件内容:

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
 <string name="app_name">数据保存</string> 
 <string name="file_name">文件名</string> 
 <string name="file_content">文件内容</string> 
 <string name="button_file_save">保存</string> 
 <string name="button_file_read">读取</string> 
 <string name="file_save_success">保存文件成功</string> 
 <string name="file_save_failed">保存文件失败</string> 
 <string name="file_read_failed">读取文件失败</string> 
</resources>

main.xml文件内容:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://www.easck.com/apk/res/android" 
  android:orientation="vertical" android:layout_width="fill_parent" 
  android:layout_height="fill_parent"> 
  <!-- 文件名 --> 
  <TextView android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:text="@string/file_name" /> 
  <EditText android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:id="@+id/et_file_name" /> 
  <TextView android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:text="@string/file_content" /> 
  <EditText android:layout_width="fill_parent" 
    android:layout_height="wrap_content" android:minLines="3" 
    android:id="@+id/et_file_content" /> 
  <RelativeLayout android:layout_width="fill_parent" 
    android:layout_height="wrap_content"> 
    <Button android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:text="@string/button_file_save" 
      android:id="@+id/bt_save" /> 
    <Button android:layout_width="wrap_content" 
      android:layout_height="wrap_content" android:layout_toRightOf="@id/bt_save" 
      android:text="@string/button_file_read" android:id="@+id/bt_read" 
      android:layout_alignTop="@id/bt_save" 
      /> 
  </RelativeLayout> 
</LinearLayout>