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

Reply via email to