功能介紹
1.文件或者目錄重命名
2.文件拷貝操作
3.創建文件
4.創建文件目錄
5.將文件名稱排序
6.將文件名稱排序
7.將文件大小排序
8.刪除文件或者目錄下文件(包括目錄)
==============================================文件工具類
import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class FileUtil { private static final Logger logger = LoggerFactory.getLogger(FileUtil.class); /** * 文件或者目錄重命名 * @param oldFilePath 舊文件路徑 * @param newName 新的文件名,可以是單個文件名和絕對路徑 * @return */ public static boolean renameTo(String oldFilePath, String newName) { try { File oldFile = new File(oldFilePath); //若文件存在 if(oldFile.exists()){ //判斷是全路徑還是文件名 if (newName.indexOf("/") < 0 && newName.indexOf("\\") < 0){ //單文件名,判斷是windows還是Linux系統 String absolutePath = oldFile.getAbsolutePath(); if(newName.indexOf("/") > 0){ //Linux系統 newName = absolutePath.substring(0, absolutePath.lastIndexOf("/") + 1) + newName; }else{ newName = absolutePath.substring(0, absolutePath.lastIndexOf("\\") + 1) + newName; } } File file = new File(newName); //判斷重命名后的文件是否存在 if(file.exists()){ logger.info("該文件已存在,不能重命名 newFilePath={}", file.getAbsolutePath()); }else{ //不存在,重命名 return oldFile.renameTo(file); } }else { logger.info("原該文件不存在,不能重命名 oldFilePath={}", oldFilePath); } } catch (Exception e) { e.printStackTrace(); } return false; } /** * 文件拷貝操作 * @param sourceFile * @param targetFile * 若目標文件目錄不存在,也會自動創建 */ public static void copy(String sourceFile, String targetFile) { File source = new File(sourceFile); File target = new File(targetFile); target.getParentFile().mkdirs(); FileInputStream fis = null; FileOutputStream fos = null; FileChannel in = null; FileChannel out = null; try { fis = new FileInputStream(source); fos = new FileOutputStream(target); in = fis.getChannel();//得到對應的文件通道 out = fos.getChannel();//得到對應的文件通道 in.transferTo(0, in.size(), out);//連接兩個通道,並且從in通道讀取,然后寫入out通道 } catch (IOException e) { e.printStackTrace(); } finally { try { if (out != null){ out.close(); } if (in != null){ in.close(); } if (fos != null){ fos.close(); } if (fis != null){ fis.close(); } } catch (IOException e) { e.printStackTrace(); } } } /** * 創建文件 * @param filePath * @return */ public static File mkFile(String filePath) { File file = new File(filePath); if(!file.exists()) { String dirPath = FilePathUtil.extractDirPath(filePath); if(mkdirs(dirPath)) { try { file.createNewFile(); logger.info("文件創建成功. filePath={}", filePath); return file; } catch (IOException e) { e.printStackTrace(); } }else { logger.info("文件目錄創建失敗. dirPath={}", dirPath); } }else { logger.info("文件已存在,無需創建. filePath={}", filePath); } return null; } /** * 創建文件目錄 * @param dirPath * @return */ public static boolean mkdirs(String dirPath) { try{ File fileDir = new File(dirPath); if(!fileDir.exists()){ fileDir.mkdirs(); logger.info("文件目錄創建成功. dirPath={}", dirPath); }else { logger.info("文件目錄已存在,無需創建. dirPath={}", dirPath); } return true; }catch(Exception e){ e.printStackTrace(); } return false; } /** * 將文件名稱排序 * @param filePath * @return */ public static List<File> sortByName(String filePath){ File[] files = new File(filePath).listFiles(); List<File> fileList = Arrays.asList(files); Collections.sort(fileList, new Comparator<File>() { public int compare(File o1, File o2) { if (o1.isDirectory() && o2.isFile()) return -1; if (o1.isFile() && o2.isDirectory()) return 1; return o1.getName().compareTo(o2.getName()); } }); return fileList; } /** * 將文件修改日期排序 * @param filePath * @return */ public static List<File> sortByDate(String filePath){ File[] files = new File(filePath).listFiles(); List<File> fileList = Arrays.asList(files); Collections.sort(fileList, new Comparator<File>() { public int compare(File f1, File f2) { long diff = f1.lastModified() - f2.lastModified(); if (diff > 0) return 1; else if (diff == 0) return 0; else return -1;//如果 if 中修改為 返回-1 同時此處修改為返回 1 排序就會是遞減 } }); return fileList; } /** * 將文件大小排序 * @param filePath * @return */ public static List<File> sortByLength(String filePath){ File[] files = new File(filePath).listFiles(); List<File> fileList = Arrays.asList(files); Collections.sort(fileList, new Comparator<File>() { public int compare(File f1, File f2) { long diff = f1.length() - f2.length(); if (diff > 0) return 1; else if (diff == 0) return 0; else return -1;//如果 if 中修改為 返回-1 同時此處修改為返回 1 排序就會是遞減 } }); return fileList; } /** * 刪除文件或者目錄下文件(包括目錄) * @param filePath * @return */ public static boolean delete (String filePath){ try{ File sourceFile = new File(filePath); if(sourceFile.isDirectory()){ for (File listFile : sourceFile.listFiles()) { delete(listFile.getAbsolutePath()); } } return sourceFile.delete(); }catch(Exception e){ e.printStackTrace(); } return false; } }
==============================================文件工具測試類
/** * 文件或者目錄重命名 */ @Test public void test_fileRename() { String oldFilePath = "E:/home/commUtils/temp/Person04"; String newName = "E:/home/commUtils/temp/Person03"; boolean renameTo = FileUtil.renameTo(oldFilePath, newName); System.out.println(renameTo); } /** * 文件拷貝 */ @Test public void test_fileCopy() { String sourceFile = "E:/home/commUtils/temp/person/Person05.txt"; String targetFile = "E:/home/commUtils/temp/person04/Person04.txt"; FileUtil.copy(sourceFile, targetFile); } /** * 文件創建 */ @Test public void test_fileCreate() { // 可以連續創建 temp person目錄 // String dirPath = "E:/home/commUtils/temp/person"; // boolean mkdirs = FileUtil.mkdirs(dirPath); // System.out.println(mkdirs); String filePath = "E:/home/commUtils/temp/person/Person05.txt"; File mkFile = FileUtil.mkFile(filePath); System.out.println(mkFile.getName()); } /** * 文件排序 */ @Test public void test_fileSort() { String filePath = "E:\\home\\commUtils\\temp"; // List<File> fileList = FileUtil.sortByLength(filePath); // List<File> fileList = FileUtil.sortByDate(filePath); List<File> fileList = FileUtil.sortByName(filePath); for(File file : fileList) { System.out.println(file.getName()); } } /** * 刪除文件或者目錄下文件(包括目錄) */ @Test public void test_delete() { String filePath = "E:\\home\\commUtils\\temp\\Person.txt"; boolean delete = FileUtil.delete(filePath); System.out.println(delete); }