java media_unmount file_(20120801)android文件的读写SD卡总结

android的SD卡的文件读取操作

首先设置xml文件的权限,和单元测试的代码

package="com.wsl.file_txt"

android:versionCode="1"

android:versionName="1.0">

android:label="@string/app_name">

1.向系统里面读写文件

package com.wsl.file_txt.FileService;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import android.content.Context;

//完成对文件的操作

public class FileService {

private Context context;

public FileService() {

// TODO Auto-generated constructor stub

}

public FileService(Context context) {

super();

this.context = context;

}

//读取文件

public String readFileContentFromSdcard(String filename){

String result;

FileInputStream fileInputStream=null;

byte[] data=new byte[1024];

int len=0;

ByteArrayOutputStream outputStream=new ByteArrayOutputStream();

try {

fileInputStream=context.openFileInput(filename);

while ((len=fileInputStream.read(data))!=-1) {

outputStream.write(data,0,len);

}

} catch (Exception e) {

// TODO: handle exception

if (fileInputStream!= null) {

try {

fileInputStream.close();

} catch (IOException e2) {

// TODO: handle exception

e.printStackTrace();

}

}

}

result=new String(outputStream.toByteArray());

return result;

}

//保存文件

public String saveFileContentToSdcard(String content){

String result="";

FileOutputStream fileOutputStream=null;

try {

fileOutputStream=context.openFileOutput("users.txt", Context.MODE_APPEND);

fileOutputStream.write(content.getBytes());

result="保存文件成功了!";

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}finally{

if (fileOutputStream!=null) {

try {

fileOutputStream.close();

} catch (IOException e) {

// TODO: handle exception

e.printStackTrace();

}

}

}

return result;

}

}

2.向sd卡里面读写文件

package com.wsl.file_txt.FileService;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import android.os.Environment;

public class FileServiceOne {

public FileServiceOne() {

// TODO Auto-generated constructor stub

}

//删除文件

public  boolean deleteFile(String fileName) {

boolean flag=false;

File file=Environment.getExternalStorageDirectory();

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

File file_path=new File(file,fileName);

flag=file_path.delete();

}

return flag;

}

//读取文件

public String readFile(String fileName){

String result="";

File file=Environment.getExternalStorageDirectory();

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

File file_path=new File(file,fileName);

FileInputStream fileInputStream=null;

byte[] data=new byte[1024];

int len=0;

ByteArrayOutputStream outputStream=new ByteArrayOutputStream();

try {

fileInputStream=new FileInputStream(file_path);

while ((len=fileInputStream.read(data))!=-1) {

outputStream.write(data,0,len);

}

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}finally{

if (fileInputStream!=null) {

try {

fileInputStream.close();

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}

}

result=new String(outputStream.toByteArray());

}

}

return result;

}

//保存文件

public String saveFileContentToSdcard(String content) {

String result="";

//获取SD卡的路径

File file=Environment.getExternalStorageDirectory();

//获取sd的路径

//String sdcard_path=file.getAbsolutePath();

//判断sd卡是否存在

if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {

FileOutputStream fileOutputStream=null;

try {

File file_path=new File(file,"bb.txt");

fileOutputStream=new FileOutputStream(file_path,true);

fileOutputStream.write(content.getBytes());

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}finally{

if (fileOutputStream!=null) {

try {

fileOutputStream.close();

} catch (Exception e) {

// TODO: handle exception

e.printStackTrace();

}

}

}

}

return result;

}

}

3.进行单元测试

package com.wsl.file_txt.Test;

import com.wsl.file_txt.FileService.FileService;

import com.wsl.file_txt.FileService.FileServiceOne;

import android.R.string;

import android.content.Context;

import android.test.AndroidTestCase;

import android.util.Log;

public class Test extends AndroidTestCase{

private String TAG="Test";

public Test() {

// TODO Auto-generated constructor stub

}

//保存单元测试

/*public void testSaveFile() {

Context context=getContext();

FileService fileService=new FileService(context);

fileService.saveFileContentToSdcard("ADFDFDFDFSFD");

}*/

/*public void testReadFile() {

Context context=getContext();

FileService fileService=new FileService(context);

Log.i("TAG", "-------->>>"+fileService.readFileContentFromSdcard("users.txt"));

}*/

public void testSaveFileone() {

FileServiceOne fileService=new FileServiceOne();

//fileService.saveFileContentToSdcard("aaaaaaaaaaaaaa你好吗?");

//Log.i("TAG", "-------->>>"+fileService.readFile("bb.txt"));

boolean flag=fileService.deleteFile("bb.txt");

Log.i("TAG", "-------->>>"+flag);

}

}


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部