我的程序有一个奇怪的问题,应该复制目录及其用户输入的内容,然后将同一目录带到另一个目录。我遇到的问题是程序确实会复制文件夹并移动它但它不会复制内容并移动它们......我注意到程序由于某种原因没有调用copyFile方法而且我无法弄清楚原因。
这是我的完整代码:
package directcopy;
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 directoryName = 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 destinationName = scanner.nextLine();
//Run method to determine if it is a directory or file
isDirFile(directoryName, destinationName);
}//end main
public static void isDirFile (String source, String destination) throws Exception{
//Create a File object for new directory in new location with same name
//as source directory
File dirFile = new File (destination, new File(source).getName());
//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(), destination);
} //end if
else {
isDirFile(entry.getAbsolutePath(), destination);
} //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 destination) 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 (destination);
//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);
bufferedDest = new BufferedOutputStream(destStream);
//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 " + destination);
//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
答案 0 :(得分:0)
在'isDirFile'方法中,'dirFile'指向目标目录,在测试中可能是空的。代码注释表明它打算扫描'source'目录,但它实际上正在扫描目的地。