Regarding the classloader: The read and save methods, respectively, reside 
in two different classes. The read method is static and called like 
ReadClass.GetProperties (i.e. without an object). The save method is part 
of a servlet class and is not static. May this cause problems?

I know practically nothing about classloaders et al, and I do not tamper 
with them anywhere else in the web app. 

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




ALEX HYDE <[EMAIL PROTECTED]> 
2005-11-14 16:46

To
Jan Andersson/Sweden/[EMAIL PROTECTED]
cc

Subject
Re: How to read and save a simple file?!?






Hi Jan,

This is one of things that's given me a headache in
the past. Take what I say with a pinch of salt as I'm
no expert. I would guess that (if your file is not at
the root of the class path) you might need to specify
a full path to the file relative to the root of the
classpath. I'm afraid this is just a guess. 

For example if your properties file was located:

mydir/ReqProFPHubNotifier.properties

then you might need to try referencing it like:

cl.getResource("mydir/ReqProFPHubNotifier.properties")

or (absolutely)

cl.getResource("/mydir/ReqProFPHubNotifier.properties")

I'm afraid I'm guessing. I think I fudged this once
just through trial and error.

Good luck

ps: you are referencing the class loader via different
classes in each case so I'm not sure if this might
have an effect.

 
--- Jan Andersson <[EMAIL PROTECTED]> wrote:

> 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
> 



 
___________________________________________________________ 
Yahoo! Model Search 2005 - Find the next catwalk superstars - 
http://uk.news.yahoo.com/hot/model-search/

Reply via email to