1:Fileservice
package cn.itcast.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;
public class FileService {
private Context context;
public FileService(Context context) {
this.context = context;
}
/**
* 以私有文件保存内容
* @param filename 文件名称
* @param content 文件内容
* @throws Exception
*/
public void saveToSDCard(String filename, String content) throws Exception{
File file = new File(Environment.getExternalStorageDirectory(), filename);
FileOutputStream outStream = new FileOutputStream(file);
outStream.write(content.getBytes());
outStream.close();
}
/**
* 以私有文件保存内容
* @param filename 文件名称
* @param content 文件内容
* @throws Exception
*/
public void save(String filename, String content) throws Exception{
FileOutputStream outStream = 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{// ctrl+shift+y / x
FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_APPEND);
outStream.write(content.getBytes());
outStream.close();
}
/**
* 保存内容,注:允许其他应用从该文件中读取内容
* @param filename 文件名称
* @param content 文件内容
* @throws Exception
*/
public void saveReadable(String filename, String content) throws Exception{
FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_WORLD_READABLE);
outStream.write(content.getBytes());
outStream.close();
}
/**
* 保存内容,注:允许其他应用往该文件写入内容
* @param filename 文件名称
* @param content 文件内容
* @throws Exception
*/
public void saveWriteable(String filename, String content) throws Exception{
FileOutputStream outStream = context.openFileOutput(filename, Context.MODE_WORLD_WRITEABLE);
outStream.write(content.getBytes());
outStream.close();
}
/**
* 保存内容,注:允许其他应用对该文件读和写
* @param filename 文件名称
* @param content 文件内容
* @throws Exception
*/
public void saveRW(String filename, String content) throws Exception{
FileOutputStream outStream = context.openFileOutput(filename,
Context.MODE_WORLD_READABLE+ Context.MODE_WORLD_WRITEABLE);
outStream.write(content.getBytes());
outStream.close();
}
/**
* 读取文件内容
* @param filename 文件名称
* @return
* @throws Exception
*/
public String readFile(String filename) throws Exception{
FileInputStream inStream = context.openFileInput(filename);
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
while( (len = inStream.read(buffer))!= -1){
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();//得到文件的二进制数据
outStream.close();
inStream.close();
return new String(data);
}
}
2:FileServiceTest
package cn.itcast.file;
import cn.itcast.service.FileService;
import android.test.AndroidTestCase;
import android.util.Log;
public class FileServiceTest extends AndroidTestCase {
private static final String TAG = "FileServiceTest";
public void testSave() throws Throwable{
FileService service = new FileService(this.getContext());
service.save("itcast.txt", "www.itcast.cn");
}
public void testReadFile() throws Throwable{
FileService service = new FileService(this.getContext());
String result = service.readFile("www.txt");
Log.i(TAG, result);
}
public void testSaveAppend() throws Throwable{
FileService service = new FileService(this.getContext());
service.saveAppend("append.txt", ",www.csdn.cn");
}
public void testSaveReadable() throws Throwable{
FileService service = new FileService(this.getContext());
service.saveReadable("readable.txt", "www.sohu.com");
}
public void testSaveWriteable() throws Throwable{
FileService service = new FileService(this.getContext());
service.saveWriteable("writeable.txt", "www.sina.com.cn");
}
public void testSaveRW() throws Throwable{
FileService service = new FileService(this.getContext());
service.saveRW("rw.txt", "www.joyo.com");
}
}
3:AceessOtherAppFileTest
package cn.itcast.other;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import android.test.AndroidTestCase;
import android.util.Log;
public class AceessOtherAppFileTest extends AndroidTestCase {
private static final String TAG = "AceessOtherAppFileTest";
//文件没有发现
public void testAccessOtherAppFile() throws Throwable{
String path = "/data/data/cn.itcast.file/files/www.txt";
File file = new File(path);
FileInputStream inStream = new FileInputStream(file);
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
while( (len = inStream.read(buffer))!= -1){
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();//得到文件的二进制数据
outStream.close();
inStream.close();
Log.i(TAG, new String(data));
}
public void testAccessOtherAppReadable() throws Throwable{
String path = "/data/data/cn.itcast.file/files/readable.txt";
File file = new File(path);
FileInputStream inStream = new FileInputStream(file);
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
while( (len = inStream.read(buffer))!= -1){
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();//得到文件的二进制数据
outStream.close();
inStream.close();
Log.i(TAG, new String(data));
}
public void testWriteOtherAppReadable() throws Throwable{
String path = "/data/data/cn.itcast.file/files/readable.txt";
File file = new File(path);
FileOutputStream outStream = new FileOutputStream(file);
outStream.write("xxxx".getBytes());
outStream.close();
}
public void testWriteOtherAppWriteable() throws Throwable{
String path = "/data/data/cn.itcast.file/files/writeable.txt";
File file = new File(path);
FileOutputStream outStream = new FileOutputStream(file);
outStream.write("liming".getBytes());
outStream.close();
}
public void testAccessOtherAppWriteable() throws Throwable{
String path = "/data/data/cn.itcast.file/files/writeable.txt";
File file = new File(path);
FileInputStream inStream = new FileInputStream(file);
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
while( (len = inStream.read(buffer))!= -1){
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();//得到文件的二进制数据
outStream.close();
inStream.close();
Log.i(TAG, new String(data));
}
public void testWriteOtherAppRW() throws Throwable{
String path = "/data/data/cn.itcast.file/files/rw.txt";
File file = new File(path);
FileOutputStream outStream = new FileOutputStream(file);
outStream.write("liming".getBytes());
outStream.close();
}
public void testAccessOtherAppRW() throws Throwable{
String path = "/data/data/cn.itcast.file/files/rw.txt";
File file = new File(path);
FileInputStream inStream = new FileInputStream(file);
byte[] buffer = new byte[1024];
int len = 0;
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
while( (len = inStream.read(buffer))!= -1){
outStream.write(buffer, 0, len);
}
byte[] data = outStream.toByteArray();//得到文件的二进制数据
outStream.close();
inStream.close();
Log.i(TAG, new String(data));
}
}
4:MainActivity:SD卡读取
package cn.itcast.file;
import cn.itcast.service.FileService;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private FileService fileService;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
fileService = new FileService(this);
Button button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText filenameText = (EditText) findViewById(R.id.filename);
EditText contentText = (EditText) findViewById(R.id.filecontent);
String filename = filenameText.getText().toString();
String content = contentText.getText().toString();
try {
//判断sdcard是否存在于手机上,并且可以进行读写访问
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
fileService.saveToSDCard(filename, content);
Toast.makeText(MainActivity.this, R.string.success, 1).show();
}else{
Toast.makeText(MainActivity.this, R.string.sdcarderror, 1).show();
}
} catch (Exception e) {
Log.e(TAG, e.toString());
Toast.makeText(MainActivity.this, R.string.error, 1).show();
}
}
});
}
}
注意:如果想获得应用程序所在目录下面的file文件夹路径可通过:this.getFilesDir(),若果想获得cache文件夹路可通过: this.getCacheDir().