0

I am trying to read contents of images folder which has three differnet images in it. I was able to locate the folder but it is not allowing me to read the contents. IT gives exception saying that file was not found (Access is Denied). I changed access permissions for myself on local system for Java folder in C drive but the issue persists. I read on this question Access is denied java.io.FileNotFoundException, that file name needs to be provided, but the path of that folder already contains file name. What am I doing wrong? I am working on a Maven project if that makes any difference. Any help is appreciated. Thanks.

        public void getImagesFolder(){

        String fileLoc = this.getClass().getResource(IMAGE_FILE).getPath();
        int indexIs =fileLoc.indexOf("images/");
        String partialPathOfFileLoc = fileLoc.substring(0, indexIs);

//      try
//      {
//          //bufferedReader = new BufferedReader(new FileReader(partialPathOfFileLoc + IMAGE_FILE_WOSLASH));
//          bufferedReader = new BufferedReader(new FileReader(fileLoc));
//          while((line = bufferedReader.readLine()) != null){
//              String imageName = line;
//              System.out.println("Image name is: "+ imageName);
//          }
//      }
//      catch (FileNotFoundException e1)
//      {
//          e1.printStackTrace();
//      }
//      catch (IOException e)
//      {
//      
//          e.printStackTrace();
//      }

        File f = new File(fileLoc);
        File[] paths;

        paths = f.listFiles();
        int filesLength =paths.length;
        System.out.println("Files length is: "+ filesLength);

        for(File path:paths)
        {
           // prints file and directory paths
           System.out.println(path);
        }
Community
  • 1
  • 1
user3044240
  • 621
  • 19
  • 33
  • 2
    `Access is denied` is the thing you need to checkout. The application probably does not have read access to the folder or file. – MadConan May 26 '15 at 18:18
  • 1
    Perhaps try .listFiles() on the directory and see what is available. If you are at the right location, but do not see the file you are looking for check permissions for the application to make sure it can read/write to the folder. – Sh4d0wsPlyr May 26 '15 at 18:29
  • as per @Sh4d0wsPlyr suggestion, I tried to run a snippet of code. Please see my edits above. But notice that I ran that code using File, not BufferedReader. Why is it that BufferedReader's FileReader is unable to find the file, while File is able to? – user3044240 May 26 '15 at 19:00
  • As per the answer given by @Saket Mittal, is it possible you have the file open somewhere else? If you are using BufferedReader, but the file is open/etc it could deny you permission. Other then that as far as I am aware there should not be a significant difference between the two. – Sh4d0wsPlyr May 26 '15 at 19:44
  • @Saket Mittal, the folder containing the file for images is closed. Also, I think File object would complain as well if the file was open, but exception is not thrown in that case. – user3044240 May 26 '15 at 20:54

1 Answers1

0

If the message of the exception claims that permission is denied then, you must first check if the permissions of the file are correct and second, if the file is currently being used by another application.

Saket Mittal
  • 3,726
  • 3
  • 29
  • 49