[英]How to Copy directory folder to with in sub directory except specified folder in java?
[英]Copy Directory/Folder Program Error (Java)
我想知道是否有人可以幫助我編寫程序,因為我不知道編寫該程序的地方。 我對該程序的步驟如下:
向用戶詢問源目錄和目的地。 源是要復制的目錄。 目標是將成為新副本父目錄的目錄。
首先,您的程序應在新位置創建一個與源目錄同名的新目錄。 (如果要復制整個磁盤,則可能需要對根目錄執行一些特殊操作。根目錄沒有父目錄,並且通常沒有名稱。)
然后,您的程序應為源目錄內容中的每個項目創建一個包含File類對象的數組
接下來,它應該迭代數組,並針對數組1中的每個項目。如果它是文件,則使用copyFile()方法將該文件復制到新目錄中。
我的代碼如下,我不理解我對該程序做錯了什么,但是無論輸出結果是“文件不存在”。
import java.io.*; import java.util.Scanner; public class DirectCopy { public static void main(String[] args) throws Exception{ //Create a new instance of scanner to get user input Scanner scanner = new Scanner (System.in); //Ask user to input the directory to be copied System.out.print("Input directory to be copied."); //Save input as String String dirName = scanner.nextLine(); //Ask user to input destination where direction will be copied System.out.print("Input destination directory will be moved to."); //Save input as String String destName = scanner.nextLine(); //Run method to determine if it is a directory or file isDirFile(dirName, destName); }//end main public static void isDirFile (String source, String dest) throws Exception{ //Create a File object for new directory in new location with same name //as source directory File dirFile = new File (dest + source); //Make the new directory dirFile.mkdirs(); //Create an array of File class objects for each item in the source //directory File[] entries; //If source directory exists if (dirFile.exists()){ //If the source directory is a directory if (dirFile.isDirectory()){ //Get the data and load the array entries = dirFile.listFiles(); //Iterate the array using alternate for statement for (File entry : entries){ if (entry.isFile()){ copyFile (entry.getAbsolutePath(), dest); } //end if else { isDirFile (entry.getAbsolutePath(), dest); } //end else if }//end for }//end if }//end if else { System.out.println("File does not exist."); } //end else } public static void copyFile (String source, String dest) throws Exception { //declare Files File sourceFile = null; File destFile = null; //declare stream variables FileInputStream sourceStream = null; FileOutputStream destStream = null; //declare buffering variables BufferedInputStream bufferedSource = null; BufferedOutputStream bufferedDest = null; try { //Create File objects for source and destination files sourceFile = new File (source); destFile = new File (dest); //Create file streams for the source and destination sourceStream = new FileInputStream(sourceFile); destStream = new FileOutputStream(destFile); //Buffer the file streams with a buffer size of 8k bufferedSource = new BufferedInputStream(sourceStream,8182); bufferedDest = new BufferedOutputStream(destStream,8182); //Use an integer to transfer data between files int transfer; //Alert user as to what is happening System.out.println("Beginning file copy:"); System.out.println("\\tCopying " + source); System.out.println("\\tTo " + dest); //Read a byte while checking for End of File (EOF) while ((transfer = bufferedSource.read()) !=-1){ //Write a byte bufferedDest.write(transfer); }//end while }//end try catch (IOException e){ e.printStackTrace(); System.out.println("An unexpected I/O error occurred."); }//end catch finally { //close file streams if (bufferedSource !=null) bufferedSource.close(); if (bufferedDest !=null) bufferedDest.close(); System.out.println("Your files have been copied correctly and " + "closed."); }//end finally }//end copyDir }//end class
假設source
和dest
類似於C:\\
和D:\\New Dest
,問題可能出在此行: File dirFile = new File (dest + source);
。
根據JavaDoc
,該構造函數將類似於一個文件,該文件是兩個字符串的串聯。 在上面的示例中,將產生類似D:\\New Dest\\C:
。
以下代碼不會失敗:
File a = new File("C:" + "D:\\New Dest");
a.mkdirs();
System.out.println(a.getAbsolutePath());
但是,似乎沒有目錄創建。 我認為您需要做的是重命名此File dirFile = new File (dest + source);
: File dirFile = new File (dest + source);
像這樣:
File dirFile = new File (dest, new File(source).getName());
它應該創建您想要的目錄結構。 請注意,對於根目錄, getName()
將為空。
編輯:根據您的評論,並如@vincent所述,您正在創建一個新目錄,將其dirFile
,您將在該目錄中放置source
指定的相同目錄結構的副本。 問題是這樣的: entries = dirFile.listFiles();
。 您說的是目錄結構的源是您創建的新文件,該文件應為空,因此entries
應為空數組。
由於數組為空,因此for
循環將立即退出(根本不會執行)。 您將需要替換以下行: entries = dirFile.listFiles();
像這樣: entries = new File(source).listFiles();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.