Meaningless Drivel is fun!*
The moose likes I/O and Streams and the fly likes Writing/appending to text file Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "Writing/appending to text file" Watch "Writing/appending to text file" New topic
Author

Writing/appending to text file

Reed Anderson
Greenhorn

Joined: May 26, 2009
Posts: 8
Hello I/O experts,

I've come to you for I/O advice. To preface, I got thrown into an R&D project without knowing Java, javascript, or anything related to the web outside of simple html, so I've come a long way in the past few days, but there are pieces of the puzzle that might seem obvious, that I just don't have.

The problem:
I would like to capture a single parameter from a httpRequest or maybe an AJAX request. I would like to write that parameter to a text file. I am using Netbeans 6.5 with the Apache Tomcat 5. This is the platform that the project will be shown on, so it's OK that it isn't "live". Currently the text file is in the "web" folder of my project folder. I have two pieces of code which I was planning on using to write the text to a file, but I'm unsure if either will work, or which is better.



or this one



I am also concerned that the file will be overwritten instead of appended. Should I create a new line using /nl inside its own oute.write or println statement?

Thank you for your help,
Reed
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

The only servlet aspect to this is how to locate the file. Using a relative file path won't work -- servlets have no concept of a "current folder".

If you want to locate the absolute path to the web root, use servletContext.getRealPath("/").

The rest of your question is pure file I/O and I'll leave it to the I/O experts to address.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60774
    
  65

P.S. I modified the subject line to curtail any urges to move this post to the Servlets forum. If you have any further questions on file addressing in servlets, please open a new topic in Servlets, and we can leave this one here for the I/O parts.
Reed Anderson
Greenhorn

Joined: May 26, 2009
Posts: 8
Thanks Bear!

The only reason I mentioned servlets is because I wasn't sure I could use generic Java I/O in a servlet. I thought there might be something servlet specific that I would have to do. It looks like either of these would work then. I'll work on it some more. If any of the file I/O guys have any comments or cautions, I'd appreciate it.

Thanks,
Reed
 
Don't get me started about those stupid light bulbs.
 
subject: Writing/appending to text file
 
Similar Threads
Can I have a response object content type = "null"?
how to write printstacktrace() to a file
file lock or synchronized ?
Lookahead with Java
Log file no longer logging...