0

I have a file watstheday.txt in my src/main/resources folder of my project as shown in the image file below.

enter image description here

I read the file through a getResourceAsStream() method of the ClassLoader and perform further actions in my code which is working perfectly. However if I try to perform a check if the file exists through the below code it always returns false.

try {
            ClassLoader classLoader = getClass().getClassLoader();
            System.out.println("!@#!@# so difficult to be simple..."+classLoader.getResource("watstheday.txt"));
            //this returns false but the file is there
            System.out.println("@#@ vertigo2 "+new File(classLoader.getResource("watstheday.txt").getFile()).isFile());
            //this ALSO returns false but the file is there
            System.out.println("@#@ vertigo2 "+new File(classLoader.getResource("watstheday.txt").getFile()).exists());
            //Giving the / to mark the root of the application though that's not required
            System.out.println("@#@ vertigo3 "+new File(classLoader.getResource("//watstheday.txt").getFile()).isFile());
            //the below code with getResourceAsStream works absolutely fine and i can read the file
            classLoader.getResourceAsStream("watstheday.txt");
            BufferedReader buf = new BufferedReader(
                    new InputStreamReader(classLoader.getResourceAsStream("watstheday.txt")));
            while (true) {
                lineJustFetched = buf.readLine();
                System.out.println(" @@#@ lineJustFetched =" + lineJustFetched);
            }
            buf.close();
        } catch (Exception e) {
            e.printStackTrace();
        }

I consulted the following posts before finally putting up this Question but couldn’t find what am I doing wrong. When I print the file name it gets printed with the complete deployment path shown as

!@#!@# so difficult to be simple... vfs=$my_server_deployment_folder_location$/helloworld/watstheday.txt
raikumardipak
  • 1,461
  • 2
  • 29
  • 49
  • Is it possible that your file exists in a zip file ? – Gaurav Feb 19 '18 at 06:19
  • 1
    fyi i tested your code on my Eclipse and got @#@ vertigo2 true @#@ vertigo2 true – Evgeniy Dorofeev Feb 19 '18 at 06:23
  • Read this post, this explains why `getResource(...).getFile()` does not return something that you can pass to a `java.io.File` constructor, because it's not the same thing: [What's the difference between a Resource, URI, URL, Path and File in Java?](https://stackoverflow.com/questions/27845223/whats-the-difference-between-a-resource-uri-url-path-and-file-in-java) – Erwin Bolwidt Feb 19 '18 at 06:23
  • 1
    File.exists() would return true if your file exists physically as an entity. It always returns false when on a virtual file system. – Gaurav Feb 19 '18 at 06:28
  • @EvgeniyDorofeev i hv tried repeatedly but it's not true for me. I can't see my own folly, – raikumardipak Feb 19 '18 at 07:27
  • @Paras the file is not a zip file and when I print the file path it does get printed correctly which I cross check in the deployment server and found it to b correct – raikumardipak Feb 19 '18 at 07:29
  • @EvgeniyDorofeev is it a windows system u r using? – raikumardipak Feb 19 '18 at 07:41
  • @EvgeniyDorofeev The file exists in a vfs as mentioned by Paras so exists() or isFile() is returning me false. I guess u must have tried it out in a standalone main application and it would return true then. – raikumardipak Feb 21 '18 at 09:58

1 Answers1

2

Resources are not files. When you develop (say, in an IDE) and haven't packaged the application yet, you may get paths of real files (somewhere in src/main/resources).

However when the application is packaged, resources are entries in an archive. They do not exist as files anymore. So do not use File with resources.

lexicore
  • 42,748
  • 17
  • 132
  • 221
  • file being used as a resource is a pretty common use case. in my case i have kept the file in the resources folder as maven says to and am able to access the file for my read operation through getResourceAsStream (). Only the classLoader.getResource("watstheday.txt").getFile()).exists() fails! – raikumardipak Feb 19 '18 at 07:40
  • 1
    I wrote about using resource as file, not file as resource. – lexicore Feb 19 '18 at 08:45