java中對於文件的操作,是再常見不過了。以下代碼是自己代碼中所用到的工具類,僅供參考。
import java.io.File; import java.io.IOException; /** * 創建新文件和目錄 * * @author Fly */ public class CreateFile { /** * 驗證字符串是否為正確路徑名的正則表達式 */ private static String matches = "[A-Za-z]:\\\\[^:?\"><*]*"; /** * 通過 sPath.matches(matches) 方法的返回值判斷是否正確 * sPath 為上傳的文件路徑字符串 */ static boolean flag = false; /** * 文件 */ static File file; /** * 根據路徑刪除指定的目錄或文件,無論存在與否 * * @param deletePath * @return */ public static boolean deleteFolder(String deletePath) { flag = false; if (deletePath.matches(matches)) { file = new File(deletePath); // 判斷目錄或文件是否存在 if (!file.exists()) { // 不存在返回 false return flag; } else { // 判斷是否為文件 if (file.isFile()) { // 為文件時調用刪除文件方法 return deleteFile(deletePath); } else { // 為目錄時調用刪除目錄方法 return deleteDirectory(deletePath); } } } else { System.out.println("要傳入正確路徑!"); return false; } } /** * 刪除單個文件 * * @param filePath 文件路徑 * @return */ public static boolean deleteFile(String filePath) { flag = false; file = new File(filePath); // 路徑為文件且不為空則進行刪除 if (file.isFile() && file.exists()) { file.delete();// 文件刪除 flag = true; } return flag; } /** * 刪除目錄(文件夾)以及目錄下的文件 * * @param dirPath * @return */ public static boolean deleteDirectory(String dirPath) { // 如果sPath不以文件分隔符結尾,自動添加文件分隔符 if (!dirPath.endsWith(File.separator)) { dirPath = dirPath + File.separator; } File dirFile = new File(dirPath); // 如果dir對應的文件不存在,或者不是一個目錄,則退出 if (!dirFile.exists() || !dirFile.isDirectory()) { return false; } flag = true; // 獲得傳入路徑下的所有文件 File[] files = dirFile.listFiles(); // 循環遍歷刪除文件夾下的所有文件(包括子目錄) if (files != null) { for (File file1 : files) { if (file1.isFile()) { // 刪除子文件 flag = deleteFile(file1.getAbsolutePath()); System.out.println(file1.getAbsolutePath() + " 刪除成功"); if (!flag) { break;// 如果刪除失敗,則跳出 } } else {// 運用遞歸,刪除子目錄 flag = deleteDirectory(file1.getAbsolutePath()); if (!flag) { break;// 如果刪除失敗,則跳出 } } } } if (!flag) { return false; } // 刪除當前目錄 return dirFile.delete(); } /** * 創建單個文件 * * @param filePath 文件所存放的路徑 * @return */ public static boolean createFile(String filePath) { File file = new File(filePath); if (file.exists()) {// 判斷文件是否存在 System.out.println("目標文件已存在" + filePath); return false; } if (filePath.endsWith(File.separator)) {// 判斷文件是否為目錄 System.out.println("目標文件不能為目錄!"); return false; } if (!file.getParentFile().exists()) {// 判斷目標文件所在的目錄是否存在 // 如果目標文件所在的文件夾不存在,則創建父文件夾 System.out.println("目標文件所在目錄不存在,准備創建它!"); if (!file.getParentFile().mkdirs()) {// 判斷創建目錄是否成功 System.out.println("創建目標文件所在的目錄失敗!"); return false; } } try { if (file.createNewFile()) {// 創建目標文件 System.out.println("創建文件成功:" + filePath); return true; } else { System.out.println("創建文件失敗!"); return false; } } catch (IOException e) {// 捕獲異常 e.printStackTrace(); System.out.println("創建文件失敗!" + e.getMessage()); return false; } } /** * 創建目錄(如果目錄存在就刪掉目錄) * * @param destDirName 目標目錄路徑 * @return */ public static boolean createDir(String destDirName) { File dir = new File(destDirName); if (dir.exists()) {// 判斷目錄是否存在 System.out.println("目標目錄已存在!"); //return false; return CreateFile.deleteDirectory(destDirName); } System.out.println("已刪除原目錄並重新創建!"); if (!destDirName.endsWith(File.separator)) {// 結尾是否以"/"結束 destDirName = destDirName + File.separator; } if (dir.mkdirs()) {// 創建目標目錄 System.out.println("創建目錄成功!" + destDirName); return true; } else { System.out.println("創建目錄失敗!"); return false; } } /** * 創建臨時文件 * * @param prefix 前綴字符串定義的文件名;必須至少有三個字符長 * @param suffix 后綴字符串定義文件的擴展名;如果為null后綴".tmp" 將被使用 * @param dirName 該目錄中的文件被創建。對於默認的臨時文件目錄nullis來傳遞 * @return 一個抽象路徑名新創建的空文件。 * @throws IllegalArgumentException -- 如果前綴參數包含少於三個字符 * @throws IOException -- 如果文件創建失敗 * @throws SecurityException -- 如果SecurityManager.checkWrite(java.lang.String)方法不允許創建一個文件 */ public static String createTempFile(String prefix, String suffix, String dirName) { File tempFile = null; if (dirName == null) {// 目錄如果為空 try { tempFile = File.createTempFile(prefix, suffix);// 在默認文件夾下創建臨時文件 return tempFile.getCanonicalPath();// 返回臨時文件的路徑 } catch (IOException e) {// 捕獲異常 e.printStackTrace(); System.out.println("創建臨時文件失敗:" + e.getMessage()); return null; } } else { // 指定目錄存在 File dir = new File(dirName);// 創建目錄 if (!dir.exists()) { // 如果目錄不存在則創建目錄 if (CreateFile.createDir(dirName)) { System.out.println("創建臨時文件失敗,不能創建臨時文件所在的目錄!"); return null; } } try { tempFile = File.createTempFile(prefix, suffix, dir);// 在指定目錄下創建臨時文件 return tempFile.getCanonicalPath();// 返回臨時文件的路徑 } catch (IOException e) {// 捕獲異常 e.printStackTrace(); System.out.println("創建臨時文件失敗!" + e.getMessage()); return null; } } } public static void main(String[] args) { String dirName = "E:/createFile/";// 創建目錄 CreateFile.createDir(dirName);// 調用方法創建目錄 String fileName = dirName + "/file1.txt";// 創建文件 CreateFile.createFile(fileName);// 調用方法創建文件 String prefix = "temp";// 創建臨時文件 String surfix = ".txt";// 后綴 for (int i = 0; i < 10; i++) {// 循環創建多個文件 System.out.println("創建臨時文件: "// 調用方法創建臨時文件 + CreateFile.createTempFile(prefix, surfix, dirName)); } } }