照旧废话不说 贴代码
文件操作类
package demo.filerw.service;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import android.content.Context;
import android.os.Environment;
/**
* 文件操作类
* @author janrone
*/
public class FileService {
private Context context;
public FileService(Context context) {
this.context = context;
}
//存储数据到文件
public void saveName(String name) throws Exception{
//context.getFilesDir();// 得到存放文件的系统目录 /data/data/<package name>/files
//context.getCacheDir(); //缓存目录 /data/data/<package name>/cache
FileOutputStream outputStream=context.openFileOutput(“deomfilerw.txt”, Context.MODE_APPEND);
outputStream.write(name.getBytes());
outputStream.close();
}
//存储数据到sdcard
public void saveNameToSDCard(String name) throws Exception{
Environment.getExternalStorageDirectory(); //得到sdcard目录
File file=new File(“/sdcard”,”demosdcard.txt”);
FileOutputStream outputStream=new FileOutputStream(file);
outputStream.write(name.getBytes());
outputStream.close();
}
// 读取数据
public String getName() throws Exception{
FileInputStream inputStream=context.openFileInput(“deomfilerw.txt”);
ByteArrayOutputStream outStream=new ByteArrayOutputStream();
byte[] buffer=new byte[1024];
int len=0;
while ((len=inputStream.read(buffer))!=-1){
outStream.write(buffer, 0, len);
}
outStream.close();
byte[] data=outStream.toByteArray();
String name=new String(data);
return name;
}
}
事件监听类可以放到外边
package demo.filerw.clicklistener;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import demo.filerw.R;
import demo.filerw.service.FileService;
public class ClickListener implements OnClickListener {
private FileService fileService;
private EditText editText;
private TextView show_text;
private Context context;
public ClickListener(Context context) {
this.context=context;
fileService = new FileService(context);
Activity activity = (Activity) context;
editText = (EditText) activity.findViewById(R.id.edit_name);
show_text = (TextView) activity.findViewById(R.id.show_text);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.ok_button:
String name=editText.getText().toString();
try {
fileService.saveName(name);
Toast.makeText(context, R.string.ok_succee, Toast.LENGTH_SHORT).show();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
//Toast.makeText(contex, R.string.ok_error, Toast.LENGTH_SHORT).show();
}
break;
case R.id.read_button:
try {
String name1 =fileService.getName();
show_text.setText(name1);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Toast.makeText(context, R.string.read_error, Toast.LENGTH_SHORT).show();
}
break;
}
}
}
Activity类文件
package demo.filerw;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import demo.filerw.clicklistener.ClickListener;
public class FileRWActivity extends Activity {
private Button ok_button;
private Button read_button;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ok_button = (Button) findViewById(R.id.ok_button);
read_button = (Button) findViewById(R.id.read_button);
ok_button.setOnClickListener(new ClickListener(this));
read_button.setOnClickListener(new ClickListener(this));
}
}
main.xml
<?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” />
<TextView
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:text=”@string/name_lable”
/>
<EditText
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
android:id=”@+id/edit_name”
/>
<LinearLayout
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:orientation=”horizontal” >
<Button
android:id=”@+id/ok_button”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:text=”@string/ok_button”
/>
<Button
android:id=”@+id/read_button”
android:layout_width=”fill_parent”
android:layout_height=”fill_parent”
android:text=”@string/read_button”
/>
</LinearLayout>
<TextView
android:id=”@+id/show_text”
android:layout_width=”fill_parent”
android:layout_height=”wrap_content”
/>
</LinearLayout>
使用单元测试来测试 FileService 类
配置单元测试和配置 SDcard 所需要的权限
<uses-sdk android:minSdkVersion=”10″ />
<!– 在SDCard中创建与删除文件权限 –>
<uses-permission android:name=”android.permission.MOUNT_UNMOUNT_FILESYSTEMS”/>
<!– 往SDCard写入数据权限 –>
<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”/>
<instrumentation
android:name=”android.test.InstrumentationTestRunner”
android:targetPackage=”demo.filerw” android:label=”ServiceTest”
/>
<application
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name” >
<uses-library android:name=”android.test.runner” />
<activity
android:label=”@string/app_name”
android:name=”.FileRWActivity” >
<intent-filter >
<action android:name=”android.intent.action.MAIN” />
<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>
测试类
package demo.filerw;
import demo.filerw.service.FileService;
import android.os.Environment;
import android.test.AndroidTestCase;
import android.util.Log;
public class FileTest extends AndroidTestCase {
private static final String TAG=”FileTest”;
//存储数据
public void testSaveName() throws Exception{
FileService fileService=new FileService(this.getContext());
fileService.saveName(“李明”);
}
public void testSaveNameToSDCard() throws Exception{
if (Environment.getExternalStorageState() == Environment.MEDIA_MOUNTED) {
FileService fileService = new FileService(this.getContext());
fileService.saveNameToSDCard(“李明和小强”);
} else {
Log.e(TAG, ”sdcard not exsit or only read”);
}
}
public void getSaveName() throws Exception{
FileService fileService=new FileService(this.getContext());
String name=fileService.getName();
Log.i(TAG, name);
}
}