Search code examples
javafilenamesfilereader

Cannot Access Folder in java


I need my program to be able to read the files inside a folder, however when I try to compile I'm getting the following error: java.io.FileNotFoundException: (Access is denied)

Here is a section of my code:

 String filename="FileCount"; //Replace this with file containing names of files to be processed
 File file=new File(filename);

Solution

  • Here's a simple example. I hope this is what you're looking for

    import java.io.File;
    
    public class Files {
        public void listFiles(){
            String folderPath = "C:/Users/*****/Desktop"; // or as required
            File file = new File(folderPath);
            File[] files = file.listFiles();
            for (File fileName : files) {
                System.out.println(fileName); //Do what you need here... I'm just printing to console.
            }
        }
    
    }