This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Java in General and the fly likes trying to use a .txt or .properties file to store variables that change often Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "trying to use a .txt or .properties file to store variables that change often" Watch "trying to use a .txt or .properties file to store variables that change often" New topic
Author

trying to use a .txt or .properties file to store variables that change often

Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

I have tried a few things to do this GWT, didnt work, and then I made a .txt file with the assignments, and used java.util.Properties to so this. It works at times and at others does not is the problem. When I do a PutMethod to one of these variables stored in the .txt file through eclipse, running it as a java app, it works fine, it finds the ConstantVariables.txt file and goes through the keys to find the correct variable.

But when I try to do this through a a webpage, and not as a java app, I get a message telling me it cannot find the entity WebContent/ConstantVariables.txt, or something along those lines. Its probably an issue with Tomcat not putting the .txt file in the right place or something like that, Tomcat always gives me problems...

Can someone tell me where the problem may be coming from? Or if there is just a simpler way to do this? Right now I have to put in code to load up the .txt file and name it p, and then to p.getKey("variableName"), and such. Is there just a way to make a .properties file, and do some kind of import or something in the java code, where I can just use variableName instead of the whole p.get("variableName") stuff? It seems like this would be something that us used often and that there should be an easier way than needing all this extra code to do this,

But the main problem is the first I mentioned, that when I try to use this through webpages it doesnt find the .txt file.

Any ideas/tips/help?


True wisdom is in knowing you know nothing - Socrates
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

Use ServletContext.getRealPath when you're using servlets / JSP pages. This will be relative to the web app folder. For instance, inside a servlet, the following will print something like "C:\Program Files\Apache\Tomcat\webapps\myWepApp\WEB-INF\myFile.properties":


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Matt Kohanek
Village Idiot
Ranch Hand

Joined: Apr 04, 2009
Posts: 483

edit - nevermind, instead of using that I just went to the necessary file right-clicked and went to properties to see the full path name laughing out loud - easier than trying to figure out why I couldnt make the above line of code work

fixed - laughing out loud

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19674
    
  18

Matt Kohanek wrote:nvrmnd ... r-clicked

Could you please Use Real Words next time?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: trying to use a .txt or .properties file to store variables that change often