• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 597
  • Last Modified:

appending data in a file

Hello people
Well i want to periodically write to a file basically its a log file that is constantly updated from time to time but i dont want the file to be overwritten rather i want to update the file like each time the file is opened for the WRITE operation i should start from the end of FILE and then write the DATA in other words i want to APPEND data to this file is there any way i can do that in JAVA please help me out

Thanks in advance
Asif Iqbal
0
asifiqbal2k
Asked:
asifiqbal2k
  • 3
  • 2
  • 2
  • +1
1 Solution
 
grim_toasterCommented:
I would suggest looking into the API documents as the first port of call, in particular the java.io package.

The class FileWriter is probably the one you're after, but look into the API's for full details, as learning how to read the API's is one of the most important things you can do!  And you should have a general knowledge of certain areas, i.e. java.util, java.lang and java.io, I'm sure there are other important areas also, but these are a good starting point.
0
 
TolsCommented:
Appending to a file can be done this way (I use this in my own logging library):

BufferedWriter logWriter = new BufferedWriter(file, true);
logWriter.write(buf);
logWriter.close();
0
 
TolsCommented:
A little correction:
BufferedWriter logWriter = new BufferedWriter(new FileWriter(file, true));
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
asifiqbal2kAuthor Commented:
Hello people
Well i want to periodically write to a file basically its a log file that is constantly updated from time to time but i dont want the file to be overwritten rather i want to update the file like each time the file is opened for the WRITE operation i should start from the end of FILE and then write the DATA in other words i want to APPEND data to this file is there any way i can do that in JAVA please help me out

Thanks in advance
Asif Iqbal
0
 
lwinkenbCommented:
Tols comment correctly answers your question.
BufferedWriter logWriter = new BufferedWriter(new FileWriter(file, true));
0
 
lwinkenbCommented:
The true parameter pasted to the FileWriter means you are opening the file to append.
0
 
asifiqbal2kAuthor Commented:
well ok now one more problem now what i want to do is write data to file by specifying a position like i want to write DATA to a particular location in the FILE is there any way to that.
0
 
lwinkenbCommented:
Only if you want to overwrite the data that's already there.  There is no way to insert data at a particular point without overwriting data though.  What you need to do is read the entire file contents into memory (into a String or a StringBuffer), then insert the text you want into the string or string buffer.  Then write that back onto the file.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now