-4

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);
vefthym
  • 7,422
  • 6
  • 32
  • 58
John Anderson
  • 17
  • 1
  • 8
  • Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: How to create a [mcve]. – GhostCat Apr 25 '17 at 08:01
  • 1
    Having said the above: most likely the filename you are using is invalid; or you actually do not have the permissions to access that entity within your file system. This isnt rocket science, you know. – GhostCat Apr 25 '17 at 08:02
  • http://stackoverflow.com/questions/31351047/cant-access-a-folder-in-java-package Follow this link I hope Your Problem is solved –  Apr 25 '17 at 08:02
  • 2
    Use [`Paths`](https://www.google.com/search?q=java+paths&sourceid=ie7&rls=com.microsoft:fr-LU:IE-Address&ie=&oe=#spf=1), the exception are more verbose with `java.nio`(not `FileNotFound` for any exception possible, you will have a specific problem at least). Then edit your question with it – AxelH Apr 25 '17 at 08:10

1 Answers1

2

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.
        }
    }

}
Joe
  • 71
  • 2