在java程式中怎樣在磁碟上建立目錄和刪除目錄
來源:互聯網
上載者:User
import java.io.*; import java.util.*; public class FileOperator { public FileOperator() { } /** * 功 能: 移動檔案(只能移動檔案) * 參 數: strSourceFileName:指定的檔案全路徑名 * strDestDir: 移動到指定的檔案夾 * 傳回值: 如果成功true;否則false */ public boolean copyTo(String strSourceFileName, String strDestDir) { File fileSource = new File(strSourceFileName); File fileDest = new File(strDestDir); // 如果源檔案不存或源檔案是檔案夾 if (!fileSource.exists() || !fileSource.isFile()) { System.out.println("錯誤: FileOperator.java copyTo函數,/n原因: 源檔案[" + strSourceFileName + "],不存在或是檔案夾!"); return false; } // 如果目標檔案夾不存在 if (!fileDest.isDirectory() || !fileDest.exists()) { if (!fileDest.mkdirs()) { System.out.println("錯誤: FileOperator.java copyTo函數,/n原因:目錄檔案夾不存,在建立目標檔案夾時失敗!"); return false; } } try { String strAbsFilename = strDestDir + File.separator + fileSource.getName(); FileInputStream fileInput = new FileInputStream(strSourceFileName); FileOutputStream fileOutput = new FileOutputStream(strAbsFilename); //System.out.println("開始拷貝檔案:"); int i = 0; int count = -1; long nWriteSize = 0; long nFileSize = fileSource.length(); byte[] data = new byte[BUFFER]; //System.out.println("FILE NAME /t/t FILE SIZE"); System.out.println(fileSource.getName() + "/t/t" + nFileSize + " byte"); while (-1 != (count = fileInput.read(data, 0, BUFFER))) { fileOutput.write(data, 0, count); nWriteSize += count; long size = (nWriteSize * 100)/nFileSize; long t = nWriteSize; String msg = null; if (size <= 100 && size >= 0) { msg = "/r拷貝檔案進度: " + size + "% /t" + "/t 已拷貝: " + t; System.out.print(msg); } else if (size > 100) { msg = "/r拷貝檔案進度: " + 100 + "% /t" + "/t 已拷貝: " + t; System.out.print(msg); } } fileInput.close(); fileOutput.close(); System.out.println("/n拷貝檔案成功!"); return true; } catch(Exception e) { System.out.println("異常資訊:["); e.printStackTrace(); System.out.println("異常資訊:]"); return false; } } /** * 功 能: 刪除指定的檔案 * 參 數: 指定絕對路徑的檔案名稱 strFileName * 傳回值: 如果刪除成功true否則false; */ public boolean delete(String strFileName) { File fileDelete = new File(strFileName); if (!fileDelete.exists() || !fileDelete.isFile()) { System.out.println("錯誤: " + strFileName + "不存在!"); return false; } return fileDelete.delete(); } /** * 功 能: 移動檔案(只能移動檔案) * 參 數: strSourceFileName: 是指定的檔案全路徑名 * strDestDir: 移動到指定的檔案夾中 * 傳回值: 如果成功true; 否則false */ public boolean moveFile(String strSourceFileName, String strDestDir) { if (copyTo(strSourceFileName, strDestDir)) return this.delete(strSourceFileName); else return false; } /** * 功 能: 建立檔案夾 * 參 數: strDir 要建立的檔案夾名稱 * 傳回值: 如果成功true;否則false */ public boolean makedir(String strDir) { File fileNew = new File(strDir); if (!fileNew.exists()) return fileNew.mkdirs(); else return true; } /** * 功 能: 刪除檔案夾 * 參 數: strDir 要刪除的檔案夾名稱 * 傳回值: 如果成功true;否則false */ public boolean rmdir(String strDir) { File rmDir = new File(strDir); if (rmDir.isDirectory() && rmDir.exists()) { String[] fileList = rmDir.list(); int size = fileList.length; for (int i = 0; i < fileList.length; i++) { String subFile = strDir + File.separator + fileList[i]; File tmp = new File(subFile); if (tmp.isFile()) tmp.delete(); else if (tmp.isDirectory()) rmdir(subFile); else System.out.println("error!"); } rmDir.delete(); } else return false; return true; } // member variable private final int BUFFER = 1024; // member variable } class Test { public static void main(String[] args) { } }