android文件工具類
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);//由于該方法是將文件已二進制數組的方式將數據存儲進去,所以第二個參數里面是一個二進制數組
這個工具類里面還有其他的許多方法在工具類里面都已經進行了注釋。
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!