You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by "Steinberger, Richard" <Ri...@mantech-ist.com> on 2005/11/14 18:00:48 UTC

RE: How to read and save a simple file?!?

My way of doing this has always been to put the path to config files in
the web.xml and then pull the path into the application through the
context.
It's very configurable as the code is moved up, and leaves the developer
free to access the file without caring where it is.

I realize there may be reasons this would not work in your situation,
but thought I would throw it out as an idea.

Rick



-----Original Message-----
From: Jan Andersson [mailto:Jan.Andersson@se.ibm.com] 
Sent: Monday, November 14, 2005 10:14 AM
To: users@tomcat.apache.org
Subject: How to read and save a simple file?!?

Hi all!

This is a simple question, which should have a simple answer. 

I have a servlet in my webapp that reads a file with properties and 
displays them. You can change the properties and then hit "Save", which 
makes the servlet write the properties to a file. The problem I'm 
experincing is that when saving the properties, the file is not found.

The code for writing goes like this.

                ClassLoader cl =
ReqProPropConfig.class.getClassLoader();
                URL propsURL = 
cl.getResource("ReqProFPHubNotifier.properties");
                FileOutputStream out = new 
FileOutputStream(propsURL.getPath());
                PrintStream printOut = new PrintStream(out);

and then a series of printOut.println writes the properties, one at a 
time, to the file.

And the code for reading:
                ClassLoader cl =
ReqProInitializer.class.getClassLoader();
                Properties properties = new Properties();
                String path = 
cl.getResource("ReqProFPHubNotifier.properties").getPath();
                String decodedPath = URLDecoder.decode(path);
                InputStream in2 = new FileInputStream(decodedPath);
                properties.load(in2);
                in2.close();
                return properties;

I use the classloader to be able to find the file without having to
point 
out exactly where it is. (As I've been told, the classloader uses the 
classpath to find the file.) The code as it stands today (e.g. the use
of 
URLDecoder) is mainly a result of trial-and-error. That is, it used to 
work but not anymore (which probably is due to testing in the real 
production environment, which isn't exactly what I used when I tested
it.)

If there's another way to read and write a file with properties 
(relocationability of the property file preserved, relative path ok) I'm

VERY interested to hear.

As you understand, I'm pretty new to both to Java and Tomcat, although I

have some ten years experience of professional programming. So this 
questions in its simplicity is really annoying me. One doesn't think of 
reading and saving a simple text file as something that should be 
complicated.

Please help!

Jan Andersson
Senior Software Engineering Specialist
IBM Rational Brand Services
+46 (0)70-793 23 02
Oddegatan 5
164 92 Stockholm

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org