tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Stratmann, Holger" <hol...@cassiopeia.de>
Subject AW: saving and opening files
Date Wed, 15 Jan 2003 14:29:10 GMT
> Hey all - the mystery is now over - I did another search and 
> found a file in:
> 
> C:\Documents and Settings\Clive\Start Menu\Programs\Apache Tomcat 4.1
> 
> Now, when file is created, it thinks it is saved at 
> C:\Program Files\Apache
> Group\Tomcat  but I guess Win2K saves under different 
> profiles - weird!

Windows s.... *g*
(yeah, ok, I'm also using Windows... :-)

Really: getAbsolutePath() returning a different path than the "real path"...
I would almost call that an "issue"!!!

> Von: mwm [mailto:tomcatuserlist@ward-murphy.co.uk]
> 
> At the risk of spoiling everybody's fun, I'd just like to 
> point out that
> you're asking for trouble by not specifying a full path for a file.

I fully agree. I also wanted to write that, but first wanted to help find
the file (sounds, lame, huh? *g*)

I personally like a solution that lets you use sensible absolute paths
without a need to "configure" them.
Configuration is also nice and often preferable, but also a lot more
trouble.

What I (often) do: Save to the directory in which the application's jar-file
is located (or a subdirectory like "data", "conf" etc.)

Here's a little trick to find the application's JAR-file (or *.class file):

in MyClass.class:

public static String getJarFilename() {
	String jarFile =
MyClass.class.getResource("MyClass.class").toString();
	int p = jarFile.indexOf("file:");
	int p2 = jarFile.indexOf('!'); 
	if (p2 < 0) return jarFile.substring(p+6); // if it's a *.class file
	return jarFile.substring(p+6, p2);         // if it's a *.jar file
}

Looks slightly different in my code, I hope I didn't break it :-))

Have fun!





> -----Urspr√ľngliche Nachricht-----
> Von: Clive De Silva [mailto:cds1@dcs.qmul.ac.uk]
> Gesendet: Mittwoch, 15. Januar 2003 14:58
> An: Tomcat Users List
> Cc: holger@cassiopeia.de; Julius Davies
> Betreff: Re: saving and opening files
> 
> 
> Hey all - the mystery is now over - I did another search and 
> found a file
> in:
> 
> C:\Documents and Settings\Clive\Start Menu\Programs\Apache Tomcat 4.1
> 
> Clearly when i searched, i didnt check documents and settings  -
> 
> Now, when file is created, it thinks it is saved at 
> C:\Program Files\Apache
> Group\Tomcat  but I guess Win2K saves under different 
> profiles - weird! Well
> thank you for all the help on this topic - much appreciated!! 
> I may have to
> resort to absolute paths.
> 
> Regards and best wishes,
> 
> Clive De Silva
> 
> ----- Original Message -----
> From: "Stratmann, Holger" <holger@cassiopeia.de>
> To: "'Tomcat Users List'" <tomcat-user@jakarta.apache.org>
> Cc: <cds1@dcs.qmul.ac.uk>
> Sent: Wednesday, January 15, 2003 12:50 PM
> Subject: AW: saving and opening files
> 
> 
> > Hi - just tried your suggestion and it returns:
> > I'm file2 and I am at C:\Program Files\Apache Group\Tomcat
> > 4.1\states.gip
> > - and guess what, it is not there!Any other ideas? ;-)
> 
> Well... that's the point where it's getting "weird".
> 
> I'm quite confident that my answer was the correct one - if 
> the file gives
> you its absolute path, then that's where it should be (and on 
> top of that,
> it's in the directory from which the JVM was started, as I 
> had expected).
> 
> I'm sure this will turn out to be some "funny" thing ;-)
> 
> I'm sure you've already searched the entire C-drive for it?
> 
> Try if Java finds it:
> 
> File file2 = new java.io.File("states.gip");
> String path = file2.getAbsolutePath();
> File testfile = new File(path);
> if (testfile.exists()) {
> System.out.println("file found at " + path);
> } else {
> System.out.println("file was supposed to be at " + path + " but
> isn't!");
> }
> 
> If this code finds your file, then don't tell me it's not 
> there :-)))))
> Just go look again and again and again ;-)
> 
> By the way:
> * "exists()" is a better way of checking for a file than catching the
> exception...
> * FileInputStream() doesn't need a file, it's also happy with a path:
>   As soon as you've found out where the file is *g*, you can 
> simplify it to
> be:
>   ObjectInputStream s1 = new ObjectInputStream(new
> FileInputStream("states.gip"));
> 
> 
> 
> Looking forward to the solution for this one :-)
> 
> Holger
> 
> > -----Urspr√ľngliche Nachricht-----
> > Von: Clive De Silva [mailto:cds1@dcs.qmul.ac.uk]
> > Gesendet: Dienstag, 14. Januar 2003 23:36
> > An: Julius Davies; Tomcat Users List
> > Betreff: Re: saving and opening files
> >
> >
> > Hi - just tried your suggestion and it returns:
> > I'm file2 and I am at C:\Program Files\Apache Group\Tomcat
> > 4.1\states.gip
> > - and guess what, it is not there!Any other ideas? ;-)
> >
> > Clive
> >
> > Von: Stratmann, Holger [mailto:holger@cassiopeia.de]
> > Gesendet: Dienstag, 14. Januar 2003 11:33
> > An: 'Tomcat Users List'
> > Cc: Julius Davies
> > Betreff: AW: saving and opening files
> >
> >
> > I *think* a file with a relative path is usually put into the
> > directory from
> > which the JVM is started.
> > (that's what I've found so far - don't know if it's *always* true)
> >
> > Well, anyway: "File" has a method "getAbsolutePath()" (or
> > something like
> > that).
> >
> > Just let the file tell you where it is :-)
> >
> > File file2 = new java.io.File("states.gip");
> > System.out.println("I'm file2 and I am at " +
> > file2.getAbsolutePath());
> >
> > HTH - Good luck!
> 
> 
> >
> >
> >
> >
> > > -----Original Message-----
> > > From: Clive De Silva [mailto:cds1@dcs.qmul.ac.uk]
> > > Sent: Monday, January 13, 2003 3:48 PM
> > > To: tomcat-user@jakarta.apache.org
> > > Subject: saving and opening files
> > >
> > >
> > > Hi, I have placed my package in the \Apache Group\Tomcat
> > > 4.1\common\classes\ folder and they are all standard Java
> > > classes [no servlets or jsps]. One of the methods invoked in
> > > my main class serializes an object and saves it to a
> > > persistent file at runtime :
> > >
> > > ---------------------Start Snip 1-----------------------
> > >   public boolean saveUser(){
> > >     File file2 = new java.io.File("states.gip");
> > >       try{
> > >         myUser.allocateUser(user);
> > >         FileOutputStream out2 = new FileOutputStream(file2);
> > >         ObjectOutputStream s2 = new ObjectOutputStream(out2);
> > >         s2.writeObject(myUser);
> > >         s2.flush();
> > >         System.out.println("You have save successfully");
> > >         return true;
> > >       }
> > >       catch(Exception e){
> > >         e.printStackTrace();
> > >         System.out.println("You have save UN-Successfully");
> > >         return false;
> > >       }
> > >   }
> > > ---------------------End Snip 1-----------------------
> > >
> > > When the class is initialised/created, it first checks to see
> > > if there is a file called states.gip and if there is, then it
> > > reads in the object, if there isnt, then it creates an object
> > > and at a latter stage saves it:
> > >
> > > ---------------------Start Snip 2-----------------------
> > >   public boolean createUser(String name){
> > >     try{//see if a user had been created and try to load it back
> > >       File file2 = new java.io.File("states.gip");
> > >       FileInputStream in = new FileInputStream(file2);
> > >       ObjectInputStream s1 = new ObjectInputStream(in);
> > >       myUser = (MainUser)s1.readObject();
> > >       user = recommenderEPG.addUser(name);
> > >       user = myUser.unallocate(user);
> > >       System.out.println("There was an old user!");
> > >       return true;
> > >     }
> > >     catch(Exception e){//user has not been created so an
> > > exception is thrown -going to create a new user
> > >       try{
> > >         myUser = new MainUser(name);
> > >         user = recommenderEPG.addUser(name);
> > >         myUser.allocateUser(user);
> > >         System.out.println("there is definitely no old user");
> > >         return true;
> > >       }
> > >       catch (DataNotFoundException ef){
> > >         return false;
> > >       }
> > >     }
> > > ---------------------End Snip 2-----------------------
> > >
> > > When the class does get created for the first time, it does
> > > not see the 'states.gip' file, which is correct, because it
> > > has not been created yet. If it is instantiated for a second
> > > or nth time, it always sees the file and loads it correctly.
> > > My Question is: where is this file stored? I can not find a
> > > physical trace of this file - i have done search after search
> > > and it is not there - yet the Java class opens it
> > correcly!!!!! Help!
> > >
> > >
> > > Regards,
> > >
> > > Clive
> > >
> > >
> > > ---
> > > Outgoing mail is certified Virus Free.
> > > Checked by AVG anti-virus system (http://www.grisoft.com).
> > > Version: 6.0.435 / Virus Database: 244 - Release Date: 30/12/2002
> > >
> >
> >
> > ---
> > Outgoing mail is certified Virus Free.
> > Checked by AVG anti-virus system (http://www.grisoft.com).
> > Version: 6.0.435 / Virus Database: 244 - Release Date: 30/12/2002
> >
> >
> > --
> > To unsubscribe, e-mail:
> > <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
> > For additional commands, e-mail:
> > <mailto:tomcat-user-help@jakarta.apache.org>
> >
> 
> --
> To unsubscribe, e-mail:
> <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
> For additional commands, e-mail:
> <mailto:tomcat-user-help@jakarta.apache.org>
> 
> 
> ---
> Outgoing mail is certified Virus Free.
> Checked by AVG anti-virus system (http://www.grisoft.com).
> Version: 6.0.435 / Virus Database: 244 - Release Date: 30/12/2002
> 

--
To unsubscribe, e-mail:   <mailto:tomcat-user-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:tomcat-user-help@jakarta.apache.org>


Mime
View raw message