Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Updating keys in a properties file.

Status
Not open for further replies.

TheObserver

Programmer
Mar 26, 2002
91
US
I want to use a GUI to open a ResourceBundle and ultimately update the keys therein. I'm having no problems opening the properties file, pulling the keys out, and then displaying them. The problem comes when I'm trying to use the same file string that I used with the RB in association with FileStreams, etc, to open the file for parsing/editing. It won't find the file at the location specified. I know the file is in the classpath because the RB is being opened, etc. Why can't I use the same string ("blah.loc.Props", for instance) to open the file "manually"?

I've also manually put in variations of the file string to see if that would work, but they did not (IE "blah/loc/Props", "blah.loc.Props.properties", etc).

Thanks for your time.
 
I can't find anything in the ResourceBundle classes that actually creates a properties file.

So either you manually created a properties file to begin with (in which case you know exactly what it's name is and where you put it), or you created a subclass of ResourceBundle with a handleGetObject method to serve up values for the keys (in which case there's no property file to find).

Can you post some code to give us more info?
 
I'm not trying to use RB to create a resource file. I already created it and can use RB to get to it. The problem is, if I try to get to the same file using file handling type classes (streams, etc), it won't find the file despite pointing at the same location.

So the question, I guess, would be why can I open a file in a specific location with RB, but not with anything else?
 
This is how the situation is looking after so further testing:

I have two code packages, one is code.stuff and the other is code.things. I'm running a class out of code.stuff and tapping into a properties file in code.things. The properties file name is AppInfo.

I reference the file with my ResourceBundle as "code.things.AppInfo", and it finds it just fine and loads the keys, etc. However, if I try to reference the same file in the same way (code.things.AppInfo) with a FileReader, it won't find the file. I checked around with some test code and for some reason it keeps looking in my user directory for the file, and since it doesn't find it, the FileReader won't load it. I've checked the System.getProperties for user.dir, java.home, and java.class.path, but they are pretty much useless. I tried creating a File object using "code.things.AppInfo" and then getting a getAbsolutePath on that, but it returned my user dir.

I also tried these steps on the actually class filename that this code is running on, and it again returned the user dir instead of the dir the class is located in.

So, basically, it looks like that the question is, how do I get the full (true) path to the same file that the RB seems to be able to get out so that I can point my FileReader at it and have it actually see the file?
 
Okay, I figured out a way to fix this and wanted to share it in case anybody else needs to do this.

I cheated a bit (for now) by setting another String var to have my OS' path separaters, rather than futz with replacing the "." in my path/filename with "/". That shouldn't add too much work, though.

First, I do a System.getProperty("java.class.path").

Then I create a StringTokenizer object using the java.class.path String I just got and split it with the delimiter for my particular system (whitespace, ":", ";", etc).

Then I create a while loop that runs until I run out of tokens or a boolean variable is set to true.

Next I try and open the token+file/pathname combo. If I succeed, I set the boolean var to true, if false, I loop back up and try another token+file/pathname combo.

Further processing is within an if(){} block that is enabled by the value of the boolean var set during the file checking.

This method may only work if you're like me and you have a file you need to get to that is buried in the classpath but you want to stay away from hard-coding it. If it's as thick as mud, post a reply.
 
You can get the absolute path for resource file using class loader:

Code:
URL fileURL = ClassLoader.getSystemClassLoader().getResource("blah.loc.Props.properties");
File p = new File(fileURL.getFile());

If your resource file is not in a jar file, you should be able to open the properties file and modify it this way.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top