复制目录/文件夹程序错误(Java)

时间:2015-03-23 09:36:50

标签: java file netbeans mkdir

我想知道是否有人可以帮助我完成我的课程,因为我不知道写错了哪里出错了。我的计划步骤如下:

  1. 询问用户源目录和目标。源是要复制的目录;目的地是将成为新副本的父目录。

  2. 首先,您的程序应在新位置创建一个与源目录同名的新目录。 (如果要复制整个磁盘,则可能需要为根目录执行一些特殊操作。根目录没有父目录,通常没有名称。)

  3. 然后你的程序应该为源目录内容中的每个项创建一个包含File类对象的数组

  4. 接下来,它应该迭代数组,并为数组中的每个项目 1.如果是文件,请使用copyFile()方法将文件复制到新目录

    1. 如果是目录,则递归调用此方法以复制目录及其所有内容。
  5. 我的代码如下,我不明白我对这个程序做错了什么,但不管输出结果是什么"文件不存在。"

    
    
    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
    
    
    

1 个答案:

答案 0 :(得分:0)

假设sourcedest类似C:\D:\New Dest,问题可能位于此行内:{{ 1}}。

根据JavaDoc,该构造函数将类似于两个字符串串联的文件。在上面的示例中,这将产生类似File dirFile = new File (dest + source);的内容。

以下代码不会失败:

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, new File(source).getName()); 将为空。

编辑:根据您的评论,并且正如@vincent所提到的,您正在创建一个新目录,称之为getName(),您将放置dirFile指定的相同目录结构的副本。 问题是:source。你是说你的目录结构的源是你创建的新文件,它应该是空的,因此,entries = dirFile.listFiles();应该是一个空数组。

由于数组为空,entries循环将立即退出(它根本不会执行)。您需要使用以下内容替换此行:forentries = dirFile.listFiles();