How to read a resource file in Java standalone applications -


i creating java application in need read templates not able to. because not able locate files proper path.

the code structure follows:

the template reading class file in src/main/java/com.prototype.main while, templates residing in src/main/java/templates

which means, when create jar 3 folders 3 folders: com templates meta-inf

now class under package how can access templates? have tried following:

  1. new file("/") in case of web application have picked root directory of application in stand alone applications user directory of system
  2. system.getproperty("user.dir") gives project directory
  3. new app().getclass().getname() shows package name separated dot

so if want read template file /templates/some.xml, how entire path of file?

thanks in advance.

an embedded resource not file, resource.

you can read these resources using class#getresource or class#getresourceasinputstream depending on needs.

for example...

url url = getclass().getresource("/templates/some.xml"); 

will return url reference named resource.


Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -