File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

File I/O

 
P. Sagdeo
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
P. Sagdeo
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3451
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6920
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
P. Sagdeo
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry, a was meant to be true or false
 
P. Sagdeo
Ranch Hand
Posts: 67
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks, turns out my problem was simply omitting the path!
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic