/**
getFilesDir().getAbsolutePath(); 获取文件的 绝对路径 地址
context.getFilesDir() ===="/data/data/cn.itcast.file/files"+fileNAME
getFilesDir()方法用于获取/data/data/cn.itcast.file/cache/files
context.getCacheDir() 与context.getFilesDir()区别
getCacheDir()方法用于获取/data/data//cache目录 用于存放缓存的 位置
getFilesDir()方法用于获取/data/data//files目录
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) { // 判断SD卡的是否可以使用
*/
public class FileService {
/**
* 保存数据到手机的ROM
*
* @param filename
* 要保存的文件名称
* @param username
* 用户名
* @param password
* 用户密码
* @throws Exception
*
* 默认情况下文件的权限是私有的 .
*/
// public static void saveToRom(Context context, String filename,
// String username, String password) throws Exception {
// // String filepath = "/data/data/cn.itcast.file/files" + filename;
// File file = new File(context.getFilesDir(), filename);
// FileOutputStream fos = new FileOutputStream(file); // name,password
// fos.write((username + "," + password).getBytes());
// fos.flush();
// fos.close();
public static void saveToRom(Context context, String filename,
String username, String password) throws Exception {
File file = new File(context.getFilesDir(), filename);
FileOutputStream fos = new FileOutputStream(file);
fos.write((username + "," + password).getBytes());
fos.flush();
fos.close();
}
/**
* 创建一个全局可读的文件
*
* @param context
* @param filename
* @param username
* @param password
* @throws Exception
*/
public static void saveToRomReadable(Context context, String filename,
String username, String password) throws Exception {
// String filepath = "/data/data/cn.itcast.file/files" + filename;
// File file = new File(context.getFilesDir(), filename);
// FileOutputStream fos = new FileOutputStream(file); // name,password
// 直接在包名目录下创建一个文件 获取到这文件的输出流
FileOutputStream fos = context.openFileOutput(filename,
Context.MODE_WORLD_READABLE);
fos.write((username + "," + password).getBytes());
fos.flush();
fos.close();
}
/**
* 创建一个全局可写的文件
*
* @param context
* @param filename
* @param username
* @param password
* @throws Exception
*/
public static void saveToRomWriteable(Context context, String filename,
String username, String password) throws Exception {
// String filepath = "/data/data/cn.itcast.file/files" + filename;
// File file = new File(context.getFilesDir(), filename);
// FileOutputStream fos = new FileOutputStream(file); // name,password
// 直接在包名目录下创建一个文件 获取到这文件的输出流
FileOutputStream fos = context.openFileOutput(filename,
Context.MODE_WORLD_WRITEABLE);
fos.write((username + "," + password).getBytes());
fos.flush();
fos.close();
}
/**
* 创建一个全局可写的文件
*
* @param context
* @param filename
* @param username
* @param password
* @throws Exception
*/
public static void saveToRomAvaival(Context context, String filename,
String username, String password) throws Exception {
// String filepath = "/data/data/cn.itcast.file/files" + filename;
// File file = new File(context.getFilesDir(), filename);
// FileOutputStream fos = new FileOutputStream(file); // name,password
// 直接在包名目录下创建一个文件 获取到这文件的输出流
FileOutputStream fos = context.openFileOutput(filename,
Context.MODE_WORLD_READABLE | Context.MODE_WORLD_WRITEABLE);
fos.write((username + "," + password).getBytes());
fos.flush();
fos.close();
}
/**
* 创建一个全局可写的文件
*
* @param context
* @param filename
* @param username
* @param password
* @throws Exception
*/
public static void saveToRomAppend(Context context, String filename,
String username, String password) throws Exception {
// 直接在包名目录下创建一个文件 获取到这文件的输出流
FileOutputStream fos = context.openFileOutput(filename,
Context.MODE_APPEND);
fos.write((username + "," + password).getBytes());
fos.flush();
fos.close();
}
/**
* 读取文件里面的数据
*
* @param context
* 上下文
* @param filename
* 文件名称
* @return
*/
public static Map readFromRom(Context context,
String filename) throws Exception {
File file = new File(context.getFilesDir(), filename);
FileInputStream fis = new FileInputStream(file);
byte[] result = StreamTools.getBytes(fis);
String[] infos = new String(result).split(",");
Map loginInfo = new HashMap();
loginInfo.put("name", infos[0]);
loginInfo.put("password", infos[1]);
return loginInfo;
}
/**
* 保存数据到应用程序的缓存目录
*
* @param filename
* 要保存的文件名称
* @param username
* 用户名
* @param password
* 用户密码
* @throws Exception
*/
public static void saveToCache(Context context, String filename,
String username, String password) throws Exception {
// String filepath = "/data/data/cn.itcast.file/files" + filename;
File file = new File(context.getCacheDir(), filename);
FileOutputStream fos = new FileOutputStream(file); // name,password
fos.write((username + "," + password).getBytes());
fos.flush();
fos.close();
}
/**
* 保存数据到sd卡.
*
* @param context
* 上下文
* @param filename
* 文件名
* @param username
* 用户名
* @param password
* 密码
* @throws Exception
*/
public static void saveToSD(Context context, String filename,
String username, String password) throws Exception {
// // 判断sd卡是否可用
// if (Environment.MEDIA_MOUNTED.equals(Environment
// .getExternalStorageState())) {
// File file = new File(Environment.getExternalStorageDirectory(),
// filename);
// FileOutputStream fos = new FileOutputStream(file);
// fos.write((username + "," + password).getBytes());
// fos.flush();
// fos.close();
// } else {
// Toast.makeText(context, "sd卡不可用,请检查sd卡的状态", 0).show();
// throw new RuntimeException("sd卡不可用");
// }
//
// }
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) { // 判断SD卡的是否可以使用
File file = new File(Environment.getExternalStorageDirectory(),
filename);
FileOutputStream fos = new FileOutputStream(file);
fos.write((username + "," + password).getBytes());
fos.flush();
fos.close();
}
}
}
/**
* 把一个inputstream里面的内容 写到一个byte[] 数组里面
* @param is
* @return
*/
public static byte[] getBytes(InputStream is) throws Exception{
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while( (len = is.read(buffer))!=-1){
bos.write(buffer, 0, len);
}
is.close();
return bos.toByteArray();
}