常用的工具類,在blog記錄一下,省的以後想要用的時候又得各種找!
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; public class FileUtil { private volatile static FileUtil instance; public static FileUtil getInstance() { if (instance == null) { synchronized(FileUtil.class) { if(instance == null) { instance = new FileUtil(); } } } return instance; } public boolean mkdirFile(String PATH){ File dir = new File(PATH); if (!dir.exists()) { return dir.mkdirs(); }else { return true; } } /** * 文件的複製 */ 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(String path,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(String path, 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(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(String oldPath, 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(); } }