0

getClassLoader().getResource("sample.txt")

I know in java the above statement try to read file from build paths either bin or WEB-INF/classes or build. But If I keep '/' before sample.txt like below statement from where it will try to read the file?

getClassLoader().getResource("/sample.txt")

  • See [this](https://stackoverflow.com/questions/2343187/loading-resources-using-getclass-getresource/41781868#41781868) SO question. – Jeroen Heier Sep 08 '18 at 20:43

1 Answers1

1

According to the documentation.

If the name begins with a '/' ('\u002f'), then the absolute name of the resource is the portion of the name following the '/'.

It also says:

Finds a resource with a given name. The rules for searching resources associated with a given class are implemented by the defining class loader of the class. This method delegates to this object's class loader. If this object was loaded by the bootstrap class loader, the method delegates to ClassLoader.getSystemResource(java.lang.String).

So it will delegate depending on which class loader you are referencing.

dolan
  • 1,716
  • 11
  • 22