1. 程式人生 > >File工具類——檔案刪除、複製、移動、重新命名

File工具類——檔案刪除、複製、移動、重新命名

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 檔案工具類
 * 
 * @author only-dream
 * 
 */
public class FileUtil {
    /**
     * 刪除檔案,可以是檔案或資料夾
     * 
     * @param fileName
     *            要刪除的檔名
     * @return 刪除成功返回true,否則返回false
     * 
@author only-dream */ public static boolean delete(String fileName) { File file = new File(fileName); if (!file.exists()) { System.out.println("刪除檔案失敗:" + fileName + "不存在!"); return false; } else { if (file.isFile())
return deleteFile(fileName); else return deleteDirectory(fileName); } } /** * 刪除單個檔案 * * @param fileName * 要刪除的檔案的檔名 * @return 單個檔案刪除成功返回true,否則返回false * @author only-dream */ public static boolean deleteFile(String fileName) { File file
= new File(fileName); // 如果檔案路徑所對應的檔案存在,並且是一個檔案,則直接刪除 if (file.exists() && file.isFile()) { if (file.delete()) { System.out.println("刪除單個檔案" + fileName + "成功!"); return true; } else { System.out.println("刪除單個檔案" + fileName + "失敗!"); return false; } } else { System.out.println("刪除單個檔案失敗:" + fileName + "不存在!"); return false; } } /** * 刪除目錄及目錄下的檔案 * * @param dir * 要刪除的目錄的檔案路徑 * @return 目錄刪除成功返回true,否則返回false * @author only-dream */ public static boolean deleteDirectory(String dir) { // 如果dir不以檔案分隔符結尾,自動新增檔案分隔符 if (!dir.endsWith(File.separator)) dir = dir + File.separator; File dirFile = new File(dir); // 如果dir對應的檔案不存在,或者不是一個目錄,則退出 if ((!dirFile.exists()) || (!dirFile.isDirectory())) { System.out.println("刪除目錄失敗:" + dir + "不存在!"); return false; } boolean flag = true; // 刪除資料夾中的所有檔案包括子目錄 File[] files = dirFile.listFiles(); for (int i = 0; i < files.length; i++) { // 刪除子檔案 if (files[i].isFile()) { flag = FileUtil.deleteFile(files[i].getAbsolutePath()); if (!flag) break; } // 刪除子目錄 else if (files[i].isDirectory()) { flag = FileUtil.deleteDirectory(files[i].getAbsolutePath()); if (!flag) break; } } if (!flag) { System.out.println("刪除目錄失敗!"); return false; } // 刪除當前目錄 if (dirFile.delete()) { System.out.println("刪除目錄" + dir + "成功!"); return true; } else { return false; } } /** * 檔名重新命名 * * @param path * 檔案的上一層目錄路徑 * @param oldFileName * 要修改的檔案舊路徑 * @param newFileName * 新的檔案路徑 * @return 成功返回true,失敗返回false * @author only-dream */ public static boolean renameFile(String path, String oldFileName, String newFileName) { if (!oldFileName.equals(newFileName)) {// 新的檔名和以前檔名不同時,才有必要進行重新命名 File oldfile = new File(path + "/" + oldFileName); File newfile = new File(path + "/" + newFileName); if (newfile.exists()) {// 若在該目錄下已經有一個檔案和新檔名相同,則不允許重新命名 System.out.println(newFileName + "已經存在!"); return false; } else { oldfile.renameTo(newfile); System.out.println("檔案修改成功"); return true; } } System.out.println("檔名稱一致"); return false; } /** * 移動檔案 * * @param filename * 檔名 * @param oldpath * 舊檔案地址 * @param newpath * 新檔案地址 * @param cover * 存在同名檔案true:覆蓋;false:不覆蓋 * */ public void changeDirectory(String filename, String oldpath, String newpath, boolean cover) { if (!oldpath.equals(newpath)) { File oldfile = new File(oldpath + "/" + filename); File newfile = new File(newpath + "/" + filename); if (newfile.exists()) {// 若在待轉移目錄下,已經存在待轉移檔案 if (cover) {// 覆蓋 oldfile.renameTo(newfile); } else { System.out.println("在新目錄下已經存在:" + filename); } } else { oldfile.renameTo(newfile); } } } /** * 複製檔案 * @param src 檔案的源路徑 * @param dest 檔案的目標路徑 * @throws IOException */ public void copyFile(String src, String dest) throws IOException { FileInputStream in = new FileInputStream(src); File file = new File(dest); if (!file.exists()){//如果沒有建立 file.createNewFile(); }else{} FileOutputStream out = new FileOutputStream(file); int c; byte buffer[] = new byte[1024]; while ((c = in.read(buffer)) != -1) { for (int i = 0; i < c; i++){ out.write(buffer[i]); } } in.close(); out.close(); } public static void main(String[] args) { // // 刪除單個檔案 // String file = "c:/test/test.txt"; // FileUtil.deleteFile(file); // System.out.println(); // 刪除一個目錄 String dir = "D:/home/web/upload/upload/files"; FileUtil.deleteDirectory(dir); // System.out.println(); // // 刪除檔案 // dir = "c:/test/test0"; // FileUtil.delete(dir); } }