1. 程式人生 > >android 檔案管理工具類

android 檔案管理工具類

package com.dejun.commonsdk.util;

import com.orhanobut.logger.Logger;

import org.greenrobot.greendao.annotation.NotNull;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.util.Locale;

/**
 * Author:DoctorWei
 * Time:2018/12/10 15:51
 * Description:檔案管理工具類 包括檔案的複製進度回撥 開啟 和遍歷
 * email:
[email protected]
*/ public class FileUtil { /** * 檔案的複製 */ public static void copyFile(String parentDir, String fileName, File originFile, CopyListener copyListener) { try { FileInputStream fileInputStream = new FileInputStream(originFile); copyFile(parentDir, fileName, fileInputStream, originFile.length(), copyListener); } catch (FileNotFoundException e) { e.printStackTrace(); } } /** * 檔案的複製 */ public static void copyFile(String parentDir, String fileName, InputStream inputStream, long totalLenth, CopyListener copyListener) { try { copyListener.startCopy(); File newFile = new File(parentDir + File.separator + fileName); FileOutputStream fileOutputStream = new FileOutputStream(newFile); byte[] data = new byte[2048]; int len = 0; long currentLenght = 0; while ((len = inputStream.read(data)) != -1) { fileOutputStream.write(data, 0, len); currentLenght += len; copyListener.progress((int) (currentLenght * 100 / totalLenth)); } copyListener.finish(newFile); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public interface CopyListener { void startCopy(); void progress(int progress); void finish(File file); } /** * 建立檔案 * * @param path 檔案所在目錄的目錄名,如/java/test/0.txt,要在當前目錄下建立一個檔名為1.txt的檔案,<br> * 則path為/java/test,fileName為1.txt * @param fileName 檔名 * @return 檔案新建成功則返回true */ public static boolean createFile(@NotNull String path,@NotNull String fileName) { File file = new File(path + File.separator + fileName); if (file.exists()) { Logger.w("新建檔案失敗:file.exist()=" + file.exists()); return false; } else { try { boolean isCreated = file.createNewFile(); return isCreated; } catch (IOException e) { e.printStackTrace(); } } return false; } /** * 刪除單個檔案 * * @param path 檔案所在路徑名 * @param fileName 檔名 * @return 刪除成功則返回true */ public static boolean deleteFile(@NotNull String path, @NotNull String fileName) { File file = new File(path + File.separator + fileName); if (file.exists()) { boolean isDeleted = file.delete(); return isDeleted; } else { return false; } } /** * 根據檔名獲得檔案的副檔名 * * @param fileName 檔名 * @return 副檔名(不帶點) */ public static String getFileSuffix(@NotNull String fileName) { int index = fileName.lastIndexOf("."); String suffix = fileName.substring(index + 1, fileName.length()); return suffix; } /** * 重新命名檔案 * * @param oldPath 舊檔案的絕對路徑 * @param newPath 新檔案的絕對路徑 * @return 檔案重新命名成功則返回true */ public static boolean renameTo(@NotNull String oldPath, @NotNull String newPath) { if (oldPath.equals(newPath)) { Logger.w( "檔案重新命名失敗:新舊檔名絕對路徑相同!"); return false; } File oldFile = new File(oldPath); File newFile = new File(newPath); boolean isSuccess = oldFile.renameTo(newFile); Logger.w("檔案重新命名是否成功:" + isSuccess); return isSuccess; } /** * 重新命名檔案 * * @param oldFile 舊檔案物件 * @param newFile 新檔案物件 * @return 檔案重新命名成功則返回true */ public static boolean renameTo(File oldFile, File newFile) { if (oldFile.equals(newFile)) { Logger.w( "檔案重新命名失敗:舊檔案物件和新檔案物件相同!"); return false; } boolean isSuccess = oldFile.renameTo(newFile); Logger.w("檔案重新命名是否成功:" + isSuccess); return isSuccess; } /** * 重新命名檔案 * * @param oldFile 舊檔案物件,File型別 * @param newName 新檔案的檔名,String型別 * @return 重新命名成功則返回true */ public static boolean renameTo(File oldFile, String newName) { File newFile = new File(oldFile.getParentFile() + File.separator + newName); boolean flag = oldFile.renameTo(newFile); return flag; } /** * 檔案大小的格式化 * * @param size 檔案大小,單位為byte * @return 檔案大小格式化後的文字 */ public static String formatSize(long size) { DecimalFormat df = new DecimalFormat("####.00"); if (size < 1024) // 小於1KB { return size + "Byte"; } else if (size < 1024 * 1024) // 小於1MB { float kSize = size / 1024f; return df.format(kSize) + "KB"; } else if (size < 1024 * 1024 * 1024) // 小於1GB { float mSize = size / 1024f / 1024f; return df.format(mSize) + "MB"; } else if (size < 1024L * 1024L * 1024L * 1024L) // 小於1TB { float gSize = size / 1024f / 1024f / 1024f; return df.format(gSize) + "GB"; } else { return "size: error"; } } /** * 獲取某個路徑下的檔案列表 * * @param path 檔案路徑 * @return 檔案列表File[] files */ public static File[] getFileList(String path) { File file = new File(path); if (file.isDirectory()) { File[] files = file.listFiles(); if (files != null) { return files; } else { return null; } } else { return null; } } /** * 獲取某個目錄下的檔案列表 * * @param directory 目錄 * @return 檔案列表File[] files */ public static File[] getFileList(File directory) { File[] files = directory.listFiles(); if (files != null) { return files; } else { return null; } } /** 取得檔案或資料夾大小 */ public static long getFileSize(File file) { long size = 0; if (!file.isDirectory()) { // 檔案 return file.length(); } File files[] = file.listFiles(); // 資料夾(遞迴) for (int i = 0; i < files.length; i++) { if (files[i].isDirectory()) { size = size + getFileSize(files[i]); } else { size = size + files[i].length(); } } return size; } /** 刪除檔案 **/ public void deleteFile(File f) { if (f.isDirectory()) { File[] files = f.listFiles(); if (files != null && files.length > 0) { for (int i = 0; i < files.length; ++i) { deleteFile(files[i]); } } } f.delete(); } }