0
try (BufferedReader rd = new BufferedReader(
    new FileReader(getClass().getResource(fileName).getFile()))) {
        prop.load(rd);
} catch (IOException ex) {
    ex.printStackTrace();
}

I am trying to read a file named database.properties,which i have put in the resources directory that i defined in the eclipse build path. enter image description here i've tried passing all kind of arguments,nothing worked.The getResources always returns either null or file not found exception. I am really confused.Can someone explain what is the problem and how does resources work in the eclipse? Values i've tried passing as the fileName: database.properties,/database.properties,/resources/database.properties and some others

Aderbal Farias
  • 989
  • 10
  • 24
Barracuda
  • 477
  • 5
  • 19
  • 2
    Here we go again. A resource is not a file. It doesn't live on the file system. So you can't use a FileReader to read it. If you need to read a resource, use Class.getResourceAsStream. It returns an InputStream. You can use an InputStreamReader to read charaters from an InputStream. – JB Nizet Apr 07 '19 at 11:50
  • @JBNizet i am using getFile(),which should return a File Path that i can use.I'll try using input. – Barracuda Apr 07 '19 at 11:51
  • @JBNizet it worked with input stream raeder,though i still don't understand why it doesn't work with FileInput since i am passing FilePath with the getFile. – Barracuda Apr 07 '19 at 11:54
  • How could a method return a valid file path for something that doesn't exist on the file system? – JB Nizet Apr 07 '19 at 11:55
  • @JBNizet i don't understand what do you mean?The file itself exists.I can tell more,this worked for me,but then i switched to my other computer and was getting this error with nulls and not found exceptions.The FileReader worked on my laptop,but for some reason didn't work on my computer.Anyway,thank you for help,now i think it works correctly. – Barracuda Apr 07 '19 at 12:15
  • 1
    A resource returned by Class.getResource is a URL pointing to a resource loadable by the class loader. The class loader loads resources from the classpath. The classpath is consituted from directories (rarely, especially in production) and from jar files. So, in most of the cases (especally once in production), the resource is just an entry of a jar file. So it doesn't exist on the file system. So you shouldn't use file IO to load it, since... it's not a file. – JB Nizet Apr 07 '19 at 12:19
  • Possible duplicate of [Reading a resource file from within jar](https://stackoverflow.com/questions/20389255/reading-a-resource-file-from-within-jar) – Progman Apr 07 '19 at 15:52

0 Answers0