手机文件存储
第一步:建立一个安卓项目
第二步:进行页面的布局:在layout文件夹main.xml中线性布局,代码如下
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/filename"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/filename"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/filecontent"
/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minLines="3"
android:id="@+id/filecontent"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
android:id="@+id/button"
/>
</LinearLayout>
第三步:在配置过程中,需要几个字符串,需要在values文件夹的string.xml中配置好
主要代码如下 <stringname="filename">文件名称</string>
<stringname="filecontent">文件内容</string>
<stringname="button">保存</string>
<stringname="success">保存完成</string>
<stringname="fail">保存失败</string>
第四步:编写MainActivity.java程序
主要代码如下:publicclass MainActivityextends Activity {
@Override
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button=(Button)this.findViewById(R.id.button);
button.setOnClickListener(new ButtonOnclickListener());
}
privatefinalclassButtonOnclickListenerimplements View.OnClickListener{
@Override
publicvoid onClick(View v) {
EditTextfilename=(EditText)findViewById(R.id.filename);
EditTextfilecontent=(EditText)findViewById(R.id.filecontent);
StringfilenameText=filename.getText().toString();
StringfilecontentText=filecontent.getText().toString();
FileServicefileservice=newFileService(getApplicationContext());
try {
fileservice.save(filenameText,filecontentText);
Toast.makeText(getApplicationContext(),R.string.success,1).show();//1.吐司对象,2.用shwo()方法显示
}catch(Exception e) {
//TODO Auto-generated catch block
Toast.makeText(getApplicationContext(),R.string.fail,1).show();
e.printStackTrace();
}
}
}
第五步:建立业务逻辑层处理文件数据的存储:
publicclassFileService {
private Contextcontext;
publicFileService(Context context) {
this.context = context;
}
/**
* 保存文件
* filenameText 文件名称
* filecontentText 文件内容
* */
publicvoid save(StringfilenameText, String filecontentText)throws Exception{
//IO
// Context.MODE_PRIVATE是指私有模式,创建出来的文件只能被本应用访问,其他应用无法访问该文件,另外采用私有模式创建文件,如果原来文件存在写入的内容会覆盖原来的文件
FileOutputStreamoutStream=context.openFileOutput(filenameText,Context.MODE_PRIVATE);
byte[]buffer=filecontentText.getBytes();
outStream.write(buffer);
outStream.close();
}
public String read(StringfilenameText)throwsException{
FileInputStreaminputStream =context.openFileInput(filenameText);
byte[] buffer=newbyte[1024];
ByteArrayOutputStreamout=newByteArrayOutputStream();
int len=0;
while((len=inputStream.read(buffer))!=-1){
//System.out.println(buffer.toString());
out.write(buffer,0, len);
}
//byte[] data=out.toByteArray();
//new String(data);
return out.toString();
}
}
注意:文件的存储只是sava()方法,而read()方法是读文件,并且默认存放文件的路径是:、
/data/data/com.sdut.myfile/files中,其中com.sdut.myfile是MainActivity.java的包,files是存储文件后自动生成的文件夹,刚开始未存储数据时,并没有filter文件夹
第六步:对于数据存储,用虚拟机来验证。对于数据的读取,用单元测试来做
第七步:单元测试:在主xml的配置前面已经论述,下面编写测试类
publicclassFileServiceTestextends AndroidTestCase {
privatestaticfinal StringTag="FileServiceTest";
publicvoidtestRead()throws Throwable{
FileServiceservice=newFileService(this.getContext());
Stringresult=service.read("test.txt");
Log.i(Tag,result);
}
}
在LogCat中观察结果:测试如果正确的话会出现text.txt中文件的内容,注意text.txt是刚才sava()保存的文件。所以默认的路径也是那里/data/data….