• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Writing/appending to text file

 
Reed Anderson
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64620
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic