【问题标题】:How to read a folder, count the files and copy to new folder如何读取文件夹,计算文件并复制到新文件夹
【发布时间】:2012-10-14 08:21:09
【问题描述】:

对我上一个问题的跟进 - Java: How to read directory folder, count and display no of files and copy to another folder?,我想读取一个文件夹,计算文件夹中的文件(任何文件类型),显示文件数,然后将其复制到新文件夹。但是,我收到以下异常:

线程“主”java.io.FileNotFoundException 中的异常:C:\project\curFolder(访问被拒绝) C:\project\newFolder 已经存在:继续处理! 在 java.io.FileInputStream.open(本机方法) 在 java.io.FileInputStream.(FileInputStream.java:120) 在 filetransfer.FileTransfer.copyFiles(FileTransfer.java:54) 在 filetransfer.FileTransfer.checkDir(FileTransfer.java:44) 在 filetransfer.FileTransfer.readDirectory(FileTransfer.java:29) 在 filetransfer.FileTransfer.main(FileTransfer.java:12) Java 结果:1 构建成功(总时间:0 秒)

请记住,我是一名学生。这是我到目前为止所做的:

public class FileTransfer {

    public static void main(String[] args) throws FileNotFoundException, IOException {
         readDirectory();
    }

public static void readDirectory() throws FileNotFoundException, IOException {
        //create new file object with location of folder
        File curFolder = new File("C:/project/curFolder/");
        int totalFiles = 0;
        //for loop to count the files in the directory using listfiles method
        for (File file : curFolder.listFiles()) {
            //determine if the file object is a file
            if (file.isFile()) {
                //count files ++
                totalFiles++;
            }
        }
        //display number of files in directory
        System.out.println("Number of files: " + totalFiles);
        checkDir();
    }

    public static void checkDir() throws FileNotFoundException, IOException {
        //check if destination directory exist, if not create directory
        //create new file object with copy folder destination
        File newFolder = new File("C:/project/newFolder/");
        //Check if folder exist: True: Println with message(true and continuing)
        if (newFolder.exists()) {
            System.out.println(newFolder + " already exist: Continuing with process!");
        } else {
            //False: Create Dir
            newFolder.mkdir();
            System.out.println(newFolder + " created!");
        }
        copyFiles();
    }

    public static void copyFiles() throws FileNotFoundException, IOException {
        //copy files from specified directory to new directory
        File fromCur = new File("C:/project/curFolder/");
        File toNew = new File("C:/project/newFolder/");
        FileInputStream from = null;
        FileOutputStream to = null;
        try {
            from = new FileInputStream(fromCur);
            to = new FileOutputStream(toNew);
            byte[] buffer = new byte[4096];
            int bytesRead;

            while ((bytesRead = from.read(buffer)) != -1) {
                to.write(buffer, 0, bytesRead);
            }
        } finally {
            if (from != null) {
                try {
                    from.close();
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            }
            if (to != null) {
                try {
                    to.close();
                } catch (IOException e) {
                    System.out.println(e.getMessage());
                }
            }
        }
    }
}

【问题讨论】:

  • 你的机器中是否存在C:/project/curFolder/
  • 我也可以完全控制它并使用管理员帐户。它没有设置为只读。

标签: java copy location directory


【解决方案1】:

错误在于以下行:

from = new FileInputStream(fromCur);

因为C:\project\curFolder 是文件夹而不是文件。根据FileInputStream 文档,您看到的异常是正确的。

抛出: FileNotFoundException - 如果文件不存在,是目录而不是常规文件,或者由于某些其他原因无法打开读取。

(我在上面引用的重点)

我相信你可能会想检查输入文件is a directory然后list the files并一一复制。

【讨论】:

    【解决方案2】:

    您要做的是复制文件夹本身,而不是其中的文件。尝试更改 copyFiles 方法的这一部分:

        File fromCur = new File("C:/project/curFolder/");
            File toNew = new File("C:/project/newFolder/");
            FileInputStream from = null;
            FileOutputStream to = null;
    
        try {
    
        to = new FileOutputStream(toNew);
        byte[] buffer = new byte[4096];
        int bytesRead;
    
        for (File fileTemp : fromCur.listFiles()) {
         if (fileTemp.isFile()) {
            from = new FileInputStream(fileTemp);
             while ((bytesRead = from.read(buffer)) != -1) {
              to.write(buffer, 0, bytesRead);
             }
         }
        }
    

    循环时,这将引用您文件夹中的文件,而不是文件夹本身。

    【讨论】:

    • 不知道我是否做错了什么,但我仍然收到同样的错误?
    • 我现在的代码和你的代码一模一样,没有例外,但是 curFolder 中的文件没有复制到 newFolder 我很抱歉,这让我很困惑
    • 关于如何解决这个问题的任何想法?
    猜你喜欢
    • 2012-10-14
    • 2020-05-01
    • 2015-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-05
    相关资源
    最近更新 更多