android的SD卡的文件读取操作

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

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.wsl.file_txt"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<!-- 添加单元测试包 -->
<uses-library android:name="android.test.runner" />

<activity android:name=".Main"
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.wsl.file_txt" android:label="Tests for My App" />


<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />

</manifest>

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);
}
}

更多相关文章

  1. cordova 插件 开发添加 android 权限
  2. Android Studio 指定签名证书文件
  3. android文件管理器--文件操作
  4. android中Bitmap图像处理 修改图片大小以及保存时的文件大小
  5. Android 读取配置文件的内容
  6. 文件上传
  7. android文件上传到服务器
  8. android 权限总结
  9. wzplayer for android V1.5.3 (新增YUV文件播放)

随机推荐

  1. android定时器
  2. APP开发实战85-帧动画
  3. Android 导入 aar包引起的Error:Failed t
  4. 在代码中获取Android(安卓)theme中的attr
  5. Android的CTS测试
  6. 探索 Android(安卓)Q:位置权限
  7. android9.0 系统默认时间修改
  8. Android Studio failed to open by givin
  9. 安卓基础到入门学习(复习笔记)
  10. android:shape的使用2