Android数据存储之Fileinput和FileOutput

来源:互联网 发布:淘宝的oz轮毂 编辑:程序博客网 时间:2024/06/05 04:28

存储数据至自带的存储空进

阶段一.界面布局

采用了分层线性布局,代码如下:

<LinearLayout 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"    android:orientation="vertical"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context=".MainActivity" >   <LinearLayout    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:orientation="vertical" >    <TextView                android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/name" />    <EditText         android:id="@+id/name"        android:layout_width="fill_parent"        android:layout_height="wrap_content"/>         <TextView                android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/context" />    <EditText         android:id="@+id/context"        android:layout_width="fill_parent"        android:minLines="5"        android:layout_height="wrap_content"/>    </LinearLayout>    <LinearLayout         android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal"        >    <Button         android:id="@+id/save"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/button1"/>      <Button         android:id="@+id/read"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="@string/button2"/>                </LinearLayout></LinearLayout>


把TextView和Buttond的文本信息都放在Strings文件中,以方便修改

阶段二.编写FileService

任然采用分层模式,把FileService和单元测试类的代码都放在业务逻辑层

FileService主要编写文件的保存和读取方法,代码如下:

public class FileService {private Context context;public FileService(Context context){super();this.context=context;}
//保存文件public void save(String filename,String fileContent) throws IOException{FileOutputStream fio=context.openFileOutput(filename, Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);fio.write(fileContent.getBytes());fio.flush();fio.close();}
//读取文件
public String read(String filename) throws IOException{FileInputStream fiI=context.openFileInput(filename);int len=0;byte[] b=new byte[1024];ByteArrayOutputStream S=new ByteArrayOutputStream();while((len=fiI.read(b))!=-1){S.write(b, 0, len);}byte[] data=S.toByteArray();S.close();fiI.close();return new String(data);}}

阶段三.编写单元测试类FileServiceTest

搭建单元测试环境,在AndroidMainFest.xml文件中添加如下代码:

  <uses-library android:name="android.test.runner"/> 写在application标签里

 <instrumentation android:name="android.test.InstrumentationTestRunner" android:targetPackage="com.example.fileop"></instrumentation> 写在application标签外

该类要继承AndroidTestCase,方法一test**命名

测试时当进度条为绿色时表示测试成功

public class FileServiceTest extends AndroidTestCase {public void testSave() throws IOException{     FileService Service=new FileService(getContext());     Service.save("hello.txt", "你好");}public void testreadFile() throws IOException{FileService Service=new FileService(getContext());String content=Service.read("hellokitty");System.out.println(content);}}


 阶段四.在确定业务逻辑层正确条件下,编写控制层

代码如下:

public class MainActivity extends Activity {private FileService fileService;private Button saveButton;private Button readButton;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        fileService=new FileService(this);        saveButton=(Button) findViewById(R.id.save);        readButton=(Button) findViewById(R.id.read);        saveButton.setOnClickListener(new View.OnClickListener() {//保存按钮的事件处理方法
@Overridepublic void onClick(View v) {// TODO Auto-generated method stubEditText fileNameText=(EditText) findViewById(R.id.name);EditText fileContentText=(EditText) findViewById(R.id.context);String fileName=fileNameText.getText().toString();String filContent=fileContentText.getText().toString();try {fileService.save(fileName, filContent);Toast.makeText(MainActivity.this, R.string.succeed, Toast.LENGTH_LONG).show();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();Toast.makeText(MainActivity.this, R.string.failure, Toast.LENGTH_LONG).show();}}});
//读取按钮的事件处理方法
        readButton.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubEditText fileNameText=(EditText) findViewById(R.id.name);EditText fileContentText=(EditText) findViewById(R.id.context);String fileName=fileNameText.getText().toString();        String fileContent = null ;//=fileContentText.getText().toString();try {fileContent=fileService.read(fileName);fileContentText.setText(fileContent);Toast.makeText(MainActivity.this, fileContent, Toast.LENGTH_LONG).show();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}});    }

阶段五.运行效果如下