This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js and have Paul Jensen on-line!
See this thread for details.
Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Reading a file without specifying any path  RSS feed

 
srinivas mahakud
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
i wanted to read a property file ,which can be in any drive.
is there a way to read it without specifying the drive.
Regards
srinivas
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I usually use the ClassLoader class to find the properties file on the classpath, since this allows the properties to move with the package without rewriting anything.

Dave
 
srinivas mahakud
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dave,
I tried ur soln.but it does not work out.
It throws some NullPointerException at
String propertiesFile = url.getFile();
its seems it is not able to find the file.
i checked the classpath too.
Regards
Srinivas

 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing I didn't mention is that it doesn't work reliably in web applications due to the way the containers mangle the classpath (or atleast I think this is the cause). Is this the problem you are having or is it something else?
Dave.
 
srinivas mahakud
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dave,
u r right,we r using it in a WebApplication.
may be this is the reason.but if u have some other
soln in this regard,plz let me know.
Regards
Srinivas
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, we're starting to get out of the realms of I/O and Streams into general code and stuff.
If the problem is the web app, something like this might work (never tried it, so if you do and it works, drop an example for us)
Servlet and JSP containers use multiple ClassLoaders for lots of different reasons. If the properties file is not on the classpath but it is on a path serviced by one of these ClassLoaders (eg WEB-INF/classes) then if you can get a Class from that path, find the ClassLoader that loaded it ( ie getClassLoader() ) then use that ClassLoader to try to find the propreties using (not sure which) either getResource or findResource.
Keep us posted?

Dave.
 
David O'Meara
Rancher
Posts: 13459
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just had to try it myself, this worked with Tomcat 4:
I'm doing the properties a little differently (via a singleton with static methods), which is the reason I have to instantiate an instance here rather than using this.getClass().getClassLoader() etc.
enjoy.
Dave.
[This message has been edited by David O'Meara (edited October 30, 2001).]
 
srinivas mahakud
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Dave,
I achieved it using ResourceBundle class.
Thanks for ur concern.
Regards
Srinivas
 
Peter den Haan
author
Ranch Hand
Posts: 3252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by David O'Meara:
[...] which is the reason I have to instantiate an instance here rather than using this.getClass().getClassLoader() etc.
You could use the class literal: MyProperties.class.getClassLoader() etc.
Maybe it's useful to mention that there are also getResource() and getResourceAsStream() methods in the Class class itself. The difference is that while the methods in ClassLoader only use absolute paths, the ones in Class can load a resource relative to the class, i.e. my.package.MyClass.class.getResourceAsStream("blah") will try to load a resource "/my/package/blah".
- Peter

[This message has been edited by Peter den Haan (edited October 31, 2001).]
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!