I have a words.txt file that I have put in a particular java package and would need to read it from that same location. I do not control the deployment, so I don’t know where the packages will be deployed.
Example location: com/example/files/words.txt .
I know how to use the ResourceBundle class to read properties file from the package hierarchy rather than a relative/absolute path. like ResourceBundle.getBundle(com.example.files.words) Is there something similar for general files so that I can read it from the package hierarchy rather than some absolute/relative path?
You can use the
getResourceAsStream()method (defined at classClass) to retrieve resources from the class-path. If classWordReaderis located in packagecom.examplethen the path to the resource file should befiles/words.txt