1

A follow up to my previous question - Java: How to read directory folder, count and display no of files and copy to another folder?, I want to read a folder, count the files in the folder (any file type), display the number of files, then copy it to a new folder. However, I am getting the following exception:

Exception in thread "main" java.io.FileNotFoundException: C:\project\curFolder (Access is denied) C:\project\newFolder already exist: Continuing with process! at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.(FileInputStream.java:120) at filetransfer.FileTransfer.copyFiles(FileTransfer.java:54) at filetransfer.FileTransfer.checkDir(FileTransfer.java:44) at filetransfer.FileTransfer.readDirectory(FileTransfer.java:29) at filetransfer.FileTransfer.main(FileTransfer.java:12) Java Result: 1 BUILD SUCCESSFUL (total time: 0 seconds)

Please keep in mind I'm a student. This is what I have done so far:

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());
                }
            }
        }
    }
}
Community
  • 1
  • 1
user1759247
  • 101
  • 1
  • 5
  • 8

2 Answers2

0

What you are trying to do is copying a folder itself, not the files inside it. Try changing this part of your copyFiles method:

    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);
         }
     }
    }

While looping, this will reference to your files inside your folder and not the folder itself.

Russell Gutierrez
  • 1,372
  • 8
  • 19
0

The error is with the following line:

from = new FileInputStream(fromCur);

because C:\project\curFolder is a folder and not a file. The exception you are seeing is correct according to the FileInputStream documentation.

Throws: FileNotFoundException - if the file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading.

(my emphasis in the above quote)

I believe you will probably want to check that the input File is a directory and then list the files and copy them one by one.

andyb
  • 43,435
  • 12
  • 121
  • 150