android 文件操作

/**


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<String, String> 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<String, String> loginInfo = new HashMap<String, String>();
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();
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值