Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

appending data in a file

Posted on 2003-11-13
8
589 Views
Last Modified: 2010-03-31
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
Comment
Question by:asifiqbal2k
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 7

Expert Comment

by:grim_toaster
ID: 9745824
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
 
LVL 2

Expert Comment

by:Tols
ID: 9745828
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
 
LVL 2

Accepted Solution

by:
Tols earned 20 total points
ID: 9745829
A little correction:
BufferedWriter logWriter = new BufferedWriter(new FileWriter(file, true));
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:asifiqbal2k
ID: 9745856
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
 
LVL 5

Expert Comment

by:lwinkenb
ID: 9745909
Tols comment correctly answers your question.
BufferedWriter logWriter = new BufferedWriter(new FileWriter(file, true));
0
 
LVL 5

Expert Comment

by:lwinkenb
ID: 9745910
The true parameter pasted to the FileWriter means you are opening the file to append.
0
 

Author Comment

by:asifiqbal2k
ID: 9754272
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
 
LVL 5

Expert Comment

by:lwinkenb
ID: 9754987
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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Spring Framework HTTPSession management 1 45
Error in @AspectJ Based AOP with Spring 2 18
Android development question 2 54
Eclipse Java import and method not resolved 4 50
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question