wood burning stoves 2.0*
The moose likes Servlets and the fly likes Problems with file i/o Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Problems with file i/o" Watch "Problems with file i/o" New topic
Author

Problems with file i/o

Micah Wedemeyer
Ranch Hand

Joined: Jun 11, 2001
Posts: 68
I'll try to give as much information as possible to make this easiest. I added some errata info at the bottom.
The problem: I cannot get my servlet to correctly read and write files. I tried just using straight out file paths, but I ran into the system-dependent/container problem of what the actual path is. To combat this, I attempted to use getContext().getRealPath(String x), but I have been having problems with that.
I am attempting to write to a file, and if it is not present, then I must create it. I heard that getRealPath will return null if the file in question does not exist or if there is a problem determining its location. In my case, getRealPath always seems to return null, even when looking for simple things. I decided to test it and used getRealPath("/index.html") and even this returned null, although I am sure the file is there.
All I need to know is how to get to my file! I am taking input from an HTML form, then putting it into a List then writing the list to a file. Each time someone new submits the form, my program reads the list from the file, adds a new node, then writes it back out. At the end of the week, I use another application to read the file and see who guessed correctly. If there is a better way to do this, please let me know! Also, I do not have access to any database tools nor do I have any DB programming experience. Besides, I will never have more than 500 entries in my list, so hardcore database stuff isn't necessary.
Notes:
First, I'm using JRun 2.3 running on a Macintosh WebSTAR 4.2 I don't know anything about either of those except JRun is nowhere near as good as Tomcat, and WebSTAR sucks compared to Apache.
Second, I can't install Tomcat and I can't get another server. As much as I'd like to, it's not my equipment, it's my boss's.
Lastly, my servlet is self-contained in a .jar file. I have heard of .war files, but I have not been able to find much information about them. I mention this because 1) I could not get JRun to run my servlet when it was extracted and 2) I read that getRealPath() gets weird when used with a .war
Kalaiselvan Selvamani
Ranch Hand

Joined: Sep 10, 2001
Posts: 32
I think the problem is getServletContext().getRealPath();
try this one
If u are using JRUN3.1 use getServletInfo().getServletContext().getRealPath()
Micah Wedemeyer
Ranch Hand

Joined: Jun 11, 2001
Posts: 68
I am using JRun 2.3 and I cannot upgrade since 3+ is not for Macintosh.
I'll give it a shot, though.
Micah
Rama Raghavan
Ranch Hand

Joined: Aug 22, 2001
Posts: 116

Try to do this
File anyFile = new File("/index.html");
Then print the path anyFile.getAbsolutePath()
You will notice that the path here may not correspond to the root of your webapp. Instead it may be a different location (may be the location of JRun server startup location..)
This might give you a clue why your file i/o is not finding the file..!!
------------------
_________________________
Rama Raghavan
SCJP2


Rama Raghavan<br />SCJP2<br />SCWCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problems with file i/o
 
Similar Threads
Having problems with Security
Reading a File in Servlet from Web App Directory
getRealPath() returns null using Weblogic
Problems getting Tomcat to work without Eclipse IDE
Is it possible to list files under certain folder with servlet