第五课,android文件操作的实例,主要对SDcard的读写操作。
[code]
package com.myclover.file.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 FileOperateService {
private Context context;
public FileOperateService(Context context) {
this.context = context;
}
/**
* 功能描述:
* 文件保存的方法
* @param fileName 文件名
* @param content 文件的内容
* @param mode 文件的模式 如果为-1时,表示的是保存到SDcard卡上,否则就是保存到应用目录中
* @throws Exception
*/
public void saveFile(String fileName , String content , int mode) throws Exception
{
FileOutputStream fouts = null;
//文件保存到应用的目录下
if(mode != -1)
{
//如果保存在应用中,则直接使用openFileOutput即可打开应用中的文件
fouts = context.openFileOutput(fileName, mode);
}
else
{
//文件保存到SDcard卡上
//Environment.getExternalStorageDirectory()表示取得SDcard的路径,相当于"/sdcard/"
File file = new File(Environment.getExternalStorageDirectory() , fileName);
fouts = new FileOutputStream(file);
}
fouts.write(content.getBytes());
fouts.close();
}
/**
* 功能描述:
* 读取文件内容
* @param fileName 文件名
* @param isSdcard 是否读取sdcard,为true时表示取得的是SDCard的文件内容
* @return 返回值:返回文件内容
* @throws Exception
*/
public String readFile(String fileName , boolean isSdcard)throws Exception
{
FileInputStream fins = null;
if(isSdcard)
{
File file = new File(Environment.getExternalStorageDirectory() , fileName);
fins = new FileInputStream(file);
}
else
{
fins = context.openFileInput(fileName);
}
ByteArrayOutputStream bouts = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while((len = fins.read(buf)) != -1)
{
bouts.write(buf, 0, len);
}
byte[] data = bouts.toByteArray();
fins.close();
bouts.close();
return new String(data);
}
}
[/code]
[code]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myclover.file"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".FileDemoActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 当需要进行单元测试时,需要引入该包 -->
<uses-library android:name="android.test.runner"/>
</application>
<uses-sdk android:minSdkVersion="9" />
<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- 这里的 android:targetPackage的包名要跟应用package同一个目录,也就是测试类要与应用类同一个目录-->
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.myclover.file" android:label="Test For FileDemo"/>
</manifest>
[/code]
[code]
package com.myclover.file.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 FileOperateService {
private Context context;
public FileOperateService(Context context) {
this.context = context;
}
/**
* 功能描述:
* 文件保存的方法
* @param fileName 文件名
* @param content 文件的内容
* @param mode 文件的模式 如果为-1时,表示的是保存到SDcard卡上,否则就是保存到应用目录中
* @throws Exception
*/
public void saveFile(String fileName , String content , int mode) throws Exception
{
FileOutputStream fouts = null;
//文件保存到应用的目录下
if(mode != -1)
{
//如果保存在应用中,则直接使用openFileOutput即可打开应用中的文件
fouts = context.openFileOutput(fileName, mode);
}
else
{
//文件保存到SDcard卡上
//Environment.getExternalStorageDirectory()表示取得SDcard的路径,相当于"/sdcard/"
File file = new File(Environment.getExternalStorageDirectory() , fileName);
fouts = new FileOutputStream(file);
}
fouts.write(content.getBytes());
fouts.close();
}
/**
* 功能描述:
* 读取文件内容
* @param fileName 文件名
* @param isSdcard 是否读取sdcard,为true时表示取得的是SDCard的文件内容
* @return 返回值:返回文件内容
* @throws Exception
*/
public String readFile(String fileName , boolean isSdcard)throws Exception
{
FileInputStream fins = null;
if(isSdcard)
{
File file = new File(Environment.getExternalStorageDirectory() , fileName);
fins = new FileInputStream(file);
}
else
{
fins = context.openFileInput(fileName);
}
ByteArrayOutputStream bouts = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while((len = fins.read(buf)) != -1)
{
bouts.write(buf, 0, len);
}
byte[] data = bouts.toByteArray();
fins.close();
bouts.close();
return new String(data);
}
}
[/code]
[code]
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myclover.file"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".FileDemoActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- 当需要进行单元测试时,需要引入该包 -->
<uses-library android:name="android.test.runner"/>
</application>
<uses-sdk android:minSdkVersion="9" />
<!-- 在SDCard中创建与删除文件权限 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!-- 往SDCard写入数据权限 -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<!-- 这里的 android:targetPackage的包名要跟应用package同一个目录,也就是测试类要与应用类同一个目录-->
<instrumentation android:name="android.test.InstrumentationTestRunner"
android:targetPackage="com.myclover.file" android:label="Test For FileDemo"/>
</manifest>
[/code]