android开发和Java开发差不了多少,也会有许多相同的功能。像本文提到的文件存储,在Java项目和android项目里面用到都是相同的。只是android开发的一些路径做了相应的处理。
下面就是在项目中用到的一个文件存储和读取类。
使用方法:① 判断文件是否存在
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import android.content.Context;
- import android.os.Environment;
- import android.text.TextUtils;
- /**
- * 文件工具类
- *
- * @version v 1.0.0 2015-8-22 下午10:21:03 xl Exp $
- */
- public class FileUtils {
- public File ImgCachePath;
- public File ImgSavePath;
- public File ImgSharePath;
- public File ApkSavePath;
- public File LogSavePath;
- public File ImgCapTempPath;
- public File ImgCapCutPath;
- public File ImgCacheDefaultPath;
- public static String APP_DATA_ROOT_PATH;
- public static String IMG_SAVE_PATH;
- public static String IMG_SHARE_PATH;
- public static String APK_INSTALL_PATH;
- public static String APK_LOG_PATH;
- public static String IMG_SAVE_PATH_CAP_TEMP;
- public static String IMG_SAVE_PATH_CAP_CUT;
- public static String IMG_CACHE_XUTILS_SDCARD_PATH;
- public static String IMG_CACHE_XUTILS_DEFAULT_PATH;
- public static String FINAL_IMAGE_PATH;
- public static String FINAL_TEMP_PATH;
- public static String SDPATH;
- public File XLPath;
- public Context mContext;
- private static FileUtils mInstance;
- public FileUtils(Context context) {
- mContext = context;
- }
- /**
- * 创建文件工具类示例
- *
- * @param context
- * 上下文
- * @return
- */
- public static synchronized FileUtils createInstance(Context context) {
- if (mInstance == null) {
- mInstance = new FileUtils(context);
- mInstance.initPath();
- }
- return mInstance;
- }
- /**
- * 获取文件工具类实例
- *
- * @return
- */
- public static synchronized FileUtils getInstance() {
- if (mInstance == null)
- throw new IllegalStateException("FileUtil must be create by call createInstance(Context context)");
- return mInstance;
- }
- /**
- * 初始化本地缓存路径
- */
- public void initPath() {
- if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
- SDPATH = Environment.getExternalStorageDirectory() + "/";
- IMG_SAVE_PATH = SDPATH + "**/images/save/";
- IMG_SHARE_PATH = SDPATH + "**/images/share/";
- APK_INSTALL_PATH = SDPATH + "**/app/";
- APK_LOG_PATH = SDPATH + "**/log/";
- IMG_SAVE_PATH_CAP_TEMP = SDPATH + "**/images/save/capture/**_temp/";
- IMG_SAVE_PATH_CAP_CUT = SDPATH + "**/images/save/capture/**_cut/";
- IMG_CACHE_XUTILS_SDCARD_PATH = SDPATH + "**/images/cache/";// 用于保存图片缓存吧
- IMG_CACHE_XUTILS_DEFAULT_PATH = SDPATH + "Android/data/" + mContext.getPackageName() + "/cache/imgCache/";
- APP_DATA_ROOT_PATH = getAppPath() + "**/";
- FINAL_IMAGE_PATH = APP_DATA_ROOT_PATH + "images/";
- FINAL_TEMP_PATH = APP_DATA_ROOT_PATH + "temp/";
- XLPath= new File(APP_DATA_ROOT_PATH);
- if (!**.exists()) {
- XLPath.mkdirs();
- }
- XLPath = new File(FINAL_IMAGE_PATH);
- if (!XLPath.exists()) {
- XLPath.mkdirs();
- }
- XLPath = new File(FINAL_TEMP_PATH);
- if (!XLPath.exists()) {
- XLPath.mkdirs();
- }
- // 拍照图片保存地址
- ImgCapTempPath = new File(IMG_SAVE_PATH_CAP_TEMP);
- if (!ImgCapTempPath.exists()) {
- ImgCapTempPath.mkdirs();
- }
- // 裁剪后图片保存地址
- ImgCapCutPath = new File(IMG_SAVE_PATH_CAP_CUT);
- if (!ImgCapCutPath.exists()) {
- ImgCapCutPath.mkdirs();
- }
- // 图片保存、缓存地址
- ImgSavePath = new File(IMG_SAVE_PATH);
- if (!ImgSavePath.exists()) {
- ImgSavePath.mkdirs();
- }
- // 分享图片的临时保存路径
- ImgSharePath = new File(IMG_SHARE_PATH);
- if (!ImgSharePath.exists()) {
- ImgSharePath.mkdirs();
- }
- // 检测更新时保存路径
- ApkSavePath = new File(APK_INSTALL_PATH);
- if (!ApkSavePath.exists()) {
- ApkSavePath.mkdirs();
- }
- // 异常保存路径
- LogSavePath = new File(APK_LOG_PATH);
- if (!LogSavePath.exists()) {
- LogSavePath.mkdirs();
- }
- ImgCachePath = new File(IMG_CACHE_XUTILS_SDCARD_PATH);
- if (!ImgCachePath.exists()) {
- ImgCachePath.mkdirs();
- }
- ImgCacheDefaultPath = new File(IMG_CACHE_XUTILS_DEFAULT_PATH);
- if (!ImgCacheDefaultPath.exists()) {
- ImgCacheDefaultPath.mkdirs();
- }
- }
- }
- private String getAppPath() {
- LogCatLog.i(TAG, "MyApplication-getAppPath():" + mContext.getFilesDir().getParent());
- return mContext.getFilesDir().getParent() + "/";
- }
- /**
- * [将文件保存到SDcard方法]<BR>
- * [功能详细描述]
- *
- * @param fileName
- * @param inStream
- * @throws IOException
- */
- public boolean saveFile2SDCard(String fileName, byte[] dataBytes) throws IOException {
- boolean flag = false;
- FileOutputStream fs = null;
- try {
- if (!TextUtils.isEmpty(fileName)) {
- File file = newFileWithPath(fileName.toString());
- if (file.exists()) {
- file.delete();
- LogCatLog.w(TAG, "httpFrame threadName:" + Thread.currentThread().getName() + " 文件已存在 则先删除: "
- + fileName.toString());
- }
- fs = new FileOutputStream(file);
- fs.write(dataBytes, 0, dataBytes.length);
- fs.flush();
- flag = true;
- }
- } catch (Exception e) {
- e.printStackTrace();
- } finally {
- if (fs != null)
- fs.close();
- }
- return flag;
- }
- /**
- * 创建一个文件,如果其所在目录不存在时,他的目录也会被跟着创建
- *
- * @author songdiyuan
- * @date 2015-8-24
- * @return
- */
- public File newFileWithPath(String filePath) {
- if (TextUtils.isEmpty(filePath)) {
- return null;
- }
- int index = filePath.lastIndexOf(File.separator);
- String path = "";
- if (index != -1) {
- path = filePath.substring(0, index);
- if (!TextUtils.isEmpty(path)) {
- File file = new File(path.toString());
- // 如果文件夹不存在
- if (!file.exists() && !file.isDirectory()) {
- boolean flag = file.mkdirs();
- if (flag) {
- LogCatLog.i(TAG, "httpFrame threadName:" + Thread.currentThread().getName() + " 创建文件夹成功:"
- + file.getPath());
- } else {
- LogCatLog.e(TAG, "httpFrame threadName:" + Thread.currentThread().getName() + " 创建文件夹失败:"
- + file.getPath());
- }
- }
- }
- }
- return new File(filePath);
- }
- /**
- * 判断文件是否存在
- *
- * @param strPath
- * @return
- */
- public boolean isExists(String strPath) {
- if (strPath == null) {
- return false;
- }
- final File strFile = new File(strPath);
- if (strFile.exists()) {
- return true;
- }
- return false;
- }
- }
- /**
- * 保存获取的 软件信息,设备信息和出错信息保存在SDcard中
- *
- * @param context
- * @param ex
- * @return
- */
- private String savaInfoToSD(Context context, Throwable ex) {
- String fileName = null;
- StringBuffer sb = new StringBuffer();
- for (Map.Entry<String, String> entry : obtainSimpleInfo(context)
- .entrySet()) {
- String key = entry.getKey();
- String value = entry.getValue();
- sb.append(key).append(" = ").append(value).append("\n");
- }
- sb.append(obtainExceptionInfo(ex));
- try {
- fileName = FileUtils.APK_LOG_PATH
- + paserTime(System.currentTimeMillis()) + ".log";
- FileOutputStream fos = new FileOutputStream(fileName);
- fos.write(sb.toString().getBytes());
- fos.flush();
- fos.close();
- } catch (Exception e) {
- e.printStackTrace();
- }
- return fileName;
- }
- String fileName = "***" + " " + time + "[在这里输入要存储的文件类型eg:.jpg/.pdf/.doc/.txt....]";
- String path = FileUtils.APK_INSTALL_PATH + fileName;
- FileUtils.getInstance().saveFile2SDCard(path, ArchData);//由于该方法是将文件已二进制数组的方式将数据存储进去,所以第二个参数里面是一个二进制数组
这个工具类里面还有其他的许多方法在工具类里面都已经进行了注释。
来源: http://www.phpxs.com/code/1001472/