File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Servlets and the fly likes FileOutputStream(), FileReader() do NOT accept relative path in servlet ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "FileOutputStream(), FileReader() do NOT accept relative path in servlet ?" Watch "FileOutputStream(), FileReader() do NOT accept relative path in servlet ?" New topic
Author

FileOutputStream(), FileReader() do NOT accept relative path in servlet ?

Frank Sikuluzu
Ranch Hand

Joined: Dec 16, 2003
Posts: 116
Without using any servlet, I found that
new FileOutputStream("data/output");
new FileReader("data/input");
work well.
However, when do the same thing from servlet, they do NOT work unless I give a full path like new FileOutputStream("C:\\mydir\\data\\output");
Is it true and how do we get it around ?
thanks.
Dmitry Melnik
Ranch Hand

Joined: Dec 18, 2003
Posts: 328
How about running this piece of code to find out where are you trying to create your files?
Billybob Marshall
Ranch Hand

Joined: Jan 27, 2004
Posts: 202
Originally posted by Frank Sikuluzu:
Without using any servlet, I found that
new FileOutputStream("data/output");
new FileReader("data/input");
work well.
However, when do the same thing from servlet, they do NOT work unless I give a full path like new FileOutputStream("C:\\mydir\\data\\output");
Is it true and how do we get it around ?
thanks.

You shouldn't be using relative paths in the first place. That makes your apps fragile, as it requires them to be started from a particular directory. If you want to read files, read them from the classpath:
InputStream is = getClass().getResourceAsStream("/yourfile");
If you want to write files, you can first read a config file which tells you the location the user wants them written - then write them there.
Frank Sikuluzu
Ranch Hand

Joined: Dec 16, 2003
Posts: 116
thanks lot for the response. Just want to know -- besides how to solve the problem in alternative way, why does it behave like that ? Is it supposed to be like that or is it because I miss something ?
Billybob Marshall
Ranch Hand

Joined: Jan 27, 2004
Posts: 202
Originally posted by Frank Sikuluzu:
thanks lot for the response. Just want to know -- besides how to solve the problem in alternative way, why does it behave like that ?

Because the web server is started from a directory that maybe you didn't expect. Maybe you are expecting relative directories to be relative to the calling class file? That is not the case. Relative directories are relative to the current working directory, usually where the java process was launched from.
Frank Sikuluzu
Ranch Hand

Joined: Dec 16, 2003
Posts: 116
Originally posted by Billybob Marshall:

Because the web server is started from a directory that maybe you didn't expect. Maybe you are expecting relative directories to be relative to the calling class file? That is not the case. Relative directories are relative to the current working directory, usually where the java process was launched from.

As a beginner, I would like to make sure I understand you clearly. My calling class "Test.class" is put at myApp/WEB-INF/classes/ folder and "data" folder is at is put at myApp/WEB-INF/classes/ too. So I assume it searches from myApp/WEB-INF/classes/ because Test.class is there. Are you saying the web container may search it from somewhere else ? BTW, I add "." in the classpath for the web container but hoping it can search from current directory of the calling class but it still fails. Sad. Please confirm if I understand you correctly.
Billybob Marshall
Ranch Hand

Joined: Jan 27, 2004
Posts: 202
Originally posted by Frank Sikuluzu:

My calling class "Test.class" is put at myApp/WEB-INF/classes/ folder and "data" folder is at is put at myApp/WEB-INF/classes/ too. So I assume it searches from myApp/WEB-INF/classes/ because Test.class is there. Are you saying the web container may search it from somewhere else ?

Regardless of whether it's the web/app server, or just a normal app, they are all running the 'java' process from some directory. It's nothing particular about it running as a servlet. As I already said, I'll say it again... Relative directories have nothing to do with the location of .class files - only from where you launched 'java' from. In your prior experience, I would guess you happened to launch 'java' from the same directory as your .class file(s) lived in. Never count on that.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: FileOutputStream(), FileReader() do NOT accept relative path in servlet ?
 
Similar Threads
Getting Current Path
Problem in streaming & creating files using FileOutputStream
Streams Vs Reader/Writer
Writing Encrypted Strings
How to acess file outside a War file.