NullPoInterException递归复制Java中的文件时

发布于 2025-01-23 15:45:56 字数 2581 浏览 0 评论 0原文

我们有以下练习:给定目录和文件列表,然后是目标副本,所有这些目录(递归)和 文件进入目的地。

这是我写的代码:

public static void copyFile(File source, File dest) throws IOException {

    //use DataOutputStream for its method size() to measure amount of bytes copied
    try(FileInputStream is = new FileInputStream(source);
        FileOutputStream os = new FileOutputStream(dest);
        DataOutputStream dos = new DataOutputStream(os)) {
            byte[] buffer = new byte[1000];
            int res;
            while ((res = is.read(buffer)) !=-1) {
                dos.write(buffer, 0, res);
            }
            int size1 = dos.size();
            if(size1<1024){
                System.out.println("Total of " + size1 + "B" + " were copied");
            }
            if(size1>1024){
                System.out.println("Total of " + size1/1024 + "KB" + " were copied");
            }
            if(size1>1e-6){
                System.out.println("Total of " + size1/1024/1024 + "MB" + " were copied");
            }
        }
    }

    static void recursiveCopy(File file, File Destination){
        if(file.isFile()){
            try {
                System.out.println(">Started: " + file.getAbsolutePath());
                copyFile(file, Destination);
                System.out.println(">Finished FILE : " + file.getAbsolutePath());
                return;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        for(File sub:file.listFiles()){
            String indent2 = "";
            System.out.println( indent2+ ">Started: " + sub.getAbsolutePath());
            recursiveCopy(sub, Destination);
            indent2+= "  ";
        }
    }

    static void copyAll(File[] listOfFiles, File dest) throws IOException{
        for(File f:listOfFiles){
            recursiveCopy(f, dest);
            System.out.println("        >Finished FOLDER: " + f.getAbsolutePath());
        }
    }

    public static void main(String[] args) throws IOException {
        File[] listOfFiles = new File[] {new File("resources/docs/books"),
                                         new File("resources/docs/books/some citations.txt")};
        copyAll(listOfFiles, new File("dest"));
    }

它在此消息中抛出nullpointerexception

线程中的“ main” java.lang.nullpointerexception:无法读取数组长度的例外

be Insure the Line for(file sub:file.listfiles())

有人知道我如何解决这个问题吗?所有文件和目录都存在,所以我不知道数组的哪个元素可能是null的。也许还有其他东西?

We have the following exercise: Given a list of directories and files followed by the destination copy all these directories (recursively) and
files into the destination.

Here is the code I wrote:

public static void copyFile(File source, File dest) throws IOException {

    //use DataOutputStream for its method size() to measure amount of bytes copied
    try(FileInputStream is = new FileInputStream(source);
        FileOutputStream os = new FileOutputStream(dest);
        DataOutputStream dos = new DataOutputStream(os)) {
            byte[] buffer = new byte[1000];
            int res;
            while ((res = is.read(buffer)) !=-1) {
                dos.write(buffer, 0, res);
            }
            int size1 = dos.size();
            if(size1<1024){
                System.out.println("Total of " + size1 + "B" + " were copied");
            }
            if(size1>1024){
                System.out.println("Total of " + size1/1024 + "KB" + " were copied");
            }
            if(size1>1e-6){
                System.out.println("Total of " + size1/1024/1024 + "MB" + " were copied");
            }
        }
    }

    static void recursiveCopy(File file, File Destination){
        if(file.isFile()){
            try {
                System.out.println(">Started: " + file.getAbsolutePath());
                copyFile(file, Destination);
                System.out.println(">Finished FILE : " + file.getAbsolutePath());
                return;
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        for(File sub:file.listFiles()){
            String indent2 = "";
            System.out.println( indent2+ ">Started: " + sub.getAbsolutePath());
            recursiveCopy(sub, Destination);
            indent2+= "  ";
        }
    }

    static void copyAll(File[] listOfFiles, File dest) throws IOException{
        for(File f:listOfFiles){
            recursiveCopy(f, dest);
            System.out.println("        >Finished FOLDER: " + f.getAbsolutePath());
        }
    }

    public static void main(String[] args) throws IOException {
        File[] listOfFiles = new File[] {new File("resources/docs/books"),
                                         new File("resources/docs/books/some citations.txt")};
        copyAll(listOfFiles, new File("dest"));
    }

It throws the NullPointerException with this message:

Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because " <local2> " is null

The issue starts with the line for(File sub:file.listFiles())

Does anyone know how I can resolve this issue? All the files and directories exist so I don't know which element of array could possibly be null. Perhaps there is something else?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

东风软 2025-01-30 15:45:56

在您的方法recursivecopy中,您的文件是一个文件(您将其与.filile())匹配,或者否则) >它是一个文件夹。您可以使用前循环处理。但这不能同时兼而有之,因此当它是文件时,您不得获取内部文件列表(因为没有)。在这种情况下跳过前面。

In your method recursiveCopy, your file is either a file (you match that with .isFile()) or else it is a folder. You handle that with the for-loop. But it cannot be both, so when it is a file, you must not fetch the list of files inside (since there are none). Skip the for-loop in that case.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文