【发布时间】:2015-03-23 09:36:50
【问题描述】:
我想知道是否有人可以帮助我完成我的程序,因为我不知道我在哪里写错了。我的程序步骤如下:
向用户询问源目录和目标。源是要复制的目录;目标是新副本的父目录。
首先,您的程序应该在新位置创建一个与源目录同名的新目录。 (如果要复制整个磁盘,您可能需要对根目录做一些特殊的事情。根目录没有父目录,而且通常没有名称。)
那么你的程序应该为源目录内容中的每个项目创建一个包含 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
【问题讨论】:
-
你能放一个你在 dirName 和 destName 中放什么的例子吗?我不明白您为什么要尝试迭代刚刚创建的文件夹。而且我不明白您为什么尝试使用 dest+source 创建文件夹,如果两者都是绝对路径,它将无法工作。
-
是的,很抱歉。 (dest + source) 发生的事情是我的教授为要求写了一个错误。我知道这很奇怪,但此刻我正在按照他说的去做。