wood burning stoves 2.0*
The moose likes Servlets and the fly likes 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 "File I/O" Watch "File I/O" New topic
Author

File I/O

P. Sagdeo
Ranch Hand

Joined: Nov 13, 2003
Posts: 67
Hi everyone!
I was wondering, do servlets have the ability to write to files?
If so, how? If not, how can I?
Thanks
P. Sagdeo
Ranch Hand

Joined: Nov 13, 2003
Posts: 67
I was trying to make something to the effect of a messege board, so basically the form data gets saved. Sorry for potential inclarity.
Parth
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
If you mean on the server, absolutely. Of course you have to have the proper permissions. You do it the same way you do in a java application with the java.io package. The Servlet spec mandates that the server must provide a directory to hold temporary files as well. That is stored as a File object in the context under the key javax.servlet.context.tempdir.
If you mean on the client, then it gets a little tricky but there are ways to do it under the right circumstances.


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
P. Sagdeo
Ranch Hand

Joined: Nov 13, 2003
Posts: 67
Yeah, I meant server, but for some reason it doent work, here's my code:
I'm alo wondering if the try/catches are nesacary, and where to put the myOutFile printlns. Thanks!
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
myOutFile = new PrintWriter(new FileWriter("post.txt",a));
Are you getting a compiler error? I didn't see where a was declared. No you don't need the try blocks if you don't mind any IOExceptions propagating up the chain.
Frank Carver
Sheriff

Joined: Jan 07, 1999
Posts: 6920
You are also quite likely to get an exception or other error, because you have used a filename relative to the "current directory", a concept that doesn't make much sense in a multi-user servlet application.
Try using a full path to the file you want to write.


Read about me at frankcarver.me ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
P. Sagdeo
Ranch Hand

Joined: Nov 13, 2003
Posts: 67
sorry, a was meant to be true or false
P. Sagdeo
Ranch Hand

Joined: Nov 13, 2003
Posts: 67
thanks, turns out my problem was simply omitting the path!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: File I/O