存储方式
流转化
首先 建立一个单元测试类
package com.zgd;
import android.test.AndroidTestCase;
import android.util.Log;
/**
* 继承测试类 进行测试
* @author Administrator
*
*/
public class FileTest extends AndroidTestCase {
public void testSave(){
FileService ftt=new FileService(this.getContext());
try {
ftt.save("1.txt", "wo are the wolrd,hello");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("SaveFile", "Wrong");
}
}
public void testSaveAppend(){ //测试追加
FileService ftt=new FileService(this.getContext());
try {
ftt.saveappend("fileservice.txt", "new yerk lla lal,hello");
Log.i("ReadFile", "追加成功");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("ReadFile", "追加Wrong");
}
}
public void testOtherRead(){ //测试追加
FileService ftt=new FileService(this.getContext());
try {
ftt.saveOtherReadable("fileservice.txt", "new yerk lla lal,hello");
Log.i("ReadFile", "追加成功");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("ReadFile", "追加Wrong");
}
}
public void testRead(){
FileService ftt=new FileService(this.getContext());
try {
String reads=ftt.read("bbb.txt");
Log.i("ReadFile", reads);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("ReadFile", "Wrong");
}
}
}
设置相应的权限
xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.zgd"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
<uses-library android:name="android.test.runner" />
<activity android:name=".FileMyActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.zgd" android:label="Tests for My App" />
</manifest>
mvc---C层
package com.zgd;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
* MVC 的 control 层
* @author Administrator
*
*/
public class FileMyActivity extends Activity {
/** Called when the activity is first created. */
EditText ev,ev2;
Button bt1;
FileService fss=new FileService(this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ev=(EditText) this.findViewById(R.id.edit1);
ev2=(EditText) this.findViewById(R.id.edit2);
bt1=(Button)this.findViewById(R.id.button);
bt1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(FileMyActivity.this, R.string.success, 2);
Log.i("Result", ev2.getText().toString());
String filename=ev.getText().toString();
String filecontent=ev2.getText().toString();
try {
fss.save(filename, filecontent);
Toast.makeText(FileMyActivity.this, R.string.success, 2).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(FileMyActivity.this, R.string.error, 1).show();
}
}
});
}
}
mvc 的 M层
package com.zgd;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import android.content.Context;
/**
* 对应的是 MVC的 Model层
* @author Administrator
*
*/
public class FileService {
private Context context;
public FileService(Context context){
this.context=context;
}
/**
* 私有文件保存内容
* @param filename
* @param content
* @throws Exception
*/
public void save(String filename,String content)throws Exception {
FileOutputStream outStream=this.context.openFileOutput(filename, Context.MODE_PRIVATE);//输出流
outStream.write(content.getBytes());//把 内容 转化成 字节流
outStream.close();//关闭管道
}
/**
* 追加文件保存内容
* @param filename
* @param content
* @throws Exception
*/
public void saveappend(String filename,String content)throws Exception {
FileOutputStream outStream=this.context.openFileOutput(filename, Context.MODE_APPEND);//输出流
outStream.write(content.getBytes());//把 内容 转化成 字节流
outStream.close();//关闭管道
}
/**
* 文件允许其他文件读取
* @param filename
* @param content
* @throws Exception
*/
public void saveOtherReadable(String filename,String content)throws Exception {
FileOutputStream outStream=this.context.openFileOutput(filename, Context.MODE_WORLD_READABLE);//输出流
outStream.write(content.getBytes());//把 内容 转化成 字节流
outStream.close();//关闭管道
}
public String read (String filname) throws Exception{
FileInputStream inStream =this.context.openFileInput(filname);//输入流
ByteArrayOutputStream bostream=new ByteArrayOutputStream(); //这一部分输出到 内存
byte[] buffer=new byte[1024];
int len=0;
// FinputStream.read() Returns the number of bytes actually read or -1 if no bytes were read
while((len=inStream.read(buffer))!=-1){
//len 代表读取了多少字节长度,如果这个流读完了 就 返回-1
//每次都将 流读取到 缓冲区 buffer
// 每读满一次后 ,就会将原来的 buffer 冲掉,所以要 保存到内存区
bostream.write(buffer,0,len);
}
byte [] Data =bostream.toByteArray();//得到输出流的字节数据
inStream.close();
bostream.close();
return new String(Data);
}
}
V层
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.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/filename"
/>
<EditText
android:id="@+id/edit1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/fileedit"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/filecontent"
/>
<EditText
android:id="@+id/edit2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@+string/filecontentedit"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
/>
</LinearLayout>
保存到 存储卡 里面
在测试类中加入以下方法
public void testSave3D(){
FileService ftt=new FileService(this.getContext());
try {
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Log.i("3D", " can be written ");
ftt.saveToSDCard("3dtest.txt","wo men dou shi hao hai zi la !");
}else
Log.i("3D", " can not be written ");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.e("SaveFile", "Wrong");
}
}
service 中加入 以下方法
/**
* 保存sd卡内容
* @param filename
* @param content
* @throws Exception
*/
public void saveToSDCard(String filename,String content)throws Exception {
File file=new File("/sdcard",filename);//建立文件路径
FileOutputStream outStream=new FileOutputStream(file);//输出流
outStream.write(content.getBytes());//把 内容 转化成 字节流
outStream.close();//关闭管道
//这个默认在/data/data 下
}
权限中加入 以下权限
<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>