天天看點

android-code-file

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import android.content.Context;
import android.os.Environment;
/**
 * 檔案操作輔助類
 * 如果是儲存在手機中,預設是在/data/data/(包名)/files下面
 * 如果是儲存在SD卡中,則預設路徑在/sdcard/下面
 * Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)//判斷目前手機是否含有SD卡
 *
 */
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);//Environment.getExternalStorageDirectory()擷取目前手機預設的sd卡路徑
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);
}
}