aspose file tools*
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
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: 61206
    
  66

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: 61206
    
  66

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Writing/appending to text file