Android 文件操作
Android集成了Java.IO库,所有java中基于“流”的输入输出类也可以在这里运用。
这里也展示一个简答的小例子。
- fileService.java
package com.file.save2sdcard;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import android.content.Context;
import android.os.Environment;
/**
* 文件操作服務類
* @author QT
*
*/
public class fileService {
private Context context;
public fileService(Context context) {
this.context = context;
}
public fileService() {
}
public static String readFileFromSdcard(String fileName) {
String message = "";
FileInputStream fileInputStream = null;
// 字節數組輸出流,寫到内存中,不需要關閉
ByteArrayOutputStream byteOutputStream = new ByteArrayOutputStream();
//文件
File file = new File(Environment.getExternalStorageDirectory(),
fileName);
try {
//对于外置存储卡进行读写操作,首先要确定状态
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) {
fileInputStream = new FileInputStream(file);
int len = 0;
//缓冲数组
byte[] data = new byte[1024];
while ((len = fileInputStream.read(data)) != -1) {
byteOutputStream.write(data, 0, len);
}
message = byteOutputStream.toString();
//messge=new String(byteOutputStream.toByteArray());
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return message;
}
/**
* 保存文件到sd卡
*
* @param fileName
* 文件名稱
* @param content
* 内容
* @return 文件是否保存的標志
*/
public static boolean writefileToSdcard(String fileName, String content) {
boolean flag = false;
FileOutputStream fileOutputStream = null;
// 设置文件路径和文件名
File file = new File(Environment.getExternalStorageDirectory(),
fileName);
// 如果外置存储卡处于可用状态
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) {
try {
fileOutputStream = new FileOutputStream(file);
fileOutputStream.write(content.getBytes());
flag = true;
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fileOutputStream != null) {
try {
fileOutputStream.close();
fileOutputStream = null;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
return flag;
}
}
- FileServiceText.java 测试类
package com.file.save2sdcard;
import android.content.Context;
import android.test.AndroidTestCase;
import android.util.Log;
/**
* 文件操作測試類
* @author QT
*
*/
public class FileServiceText extends AndroidTestCase {
public static String TAG="FileServiceText";
public FileServiceText() {
// TODO Auto-generated constructor stub
}
public void TestSaveFile(){
Context context=getContext();
fileService files=new fileService(context);
boolean flag=files.writefileToSdcard("hello.txt", "看不見你的笑,我怎麽睡的着");
Log.i(TAG, "---->>"+flag);
}
public void TestReadFile(){
Context context=getContext();
fileService files=new fileService(context);
String message=files.readFileFromSdcard("hello.txt");
Log.i(TAG, "---->>"+message);
}
}
- AndroidManifest.xml 添加权限和单元测试支持
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.file.save2sdcard"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="15"
android:targetSdkVersion="19" />
<!-- 添加读取外置存储卡的权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 添加andorid单元测试 -->
<instrumentation
android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.file.save2sdcard" >
</instrumentation>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<!-- 添加单元测试类库 -->
<uses-library android:name="android.test.runner" />
<activity
android:name="com.file.save2sdcard.MainActivity"
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>
</manifest>
运行结果
- 向SDCard保存数据
- 从SDCard得到数据