Monday 14, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Sunday, 6 October 2013

How to append content into existing file in Java


In this tutorials we are going to see how to append content into existing file using FileWriter in Java.
The constructor FileWriter(file,true) is used to append content into existing file.By default FileWriter(file) will replace all existing content with new content.
1.Create project called JavaMisc.
2.Create package called com.javatutorialscorner.io
Create java class called FileWriter under com.javatutorialscorner.io package.
FileWriter.java
package com.javatutorialscorner.io;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

public class FileWriter {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
BufferedWriter bw = null;
java.io.FileWriter fw = null;
String message = "This is new String going to append in the File named JavaTutorialsCorner";
File file = null;
try {
file = new File("C:\\jtc\\javatutorialscorner.txt");

if (!file.exists()) {
file.createNewFile();
}

fw = new java.io.FileWriter(file.getAbsoluteFile(),true);
bw = new BufferedWriter(fw);
bw.write(message);
bw.close();

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

The file already contains the following content.

This is test String to be written in File named JavaTutorialsCorner.

Now run the program open the file at specified location, the new String append with existing String.

This is test String to be written in File named JavaTutorialsCorner.This is new String going to append in the File named JavaTutorialsCorner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • How to write file using FileOutputStream in JavaIn this tutorials we are going to see how to write file using FileOutputStream in Java. 1.Create project called JavaMisc. 2.Create package called com.javatutorialscorner.io Create java class called FileWriter under c… Read More
  • How to Create File using Java Following example shows hoe to create file using File() constructor and file.createNewFile() method in Java createNewFile public boolean createNewFile() throws Atomically creates a new, empty file named by this abstract pa… Read More
  • How to make read-only file using Java Following example shows how to make file read-only using file.setReadOnly() method in Java. setReadOnlypublic boolean setReadOnly() Marks the file or directory named by this abstract pathname so that only read operations a… Read More
  • How to check file existence in Java Following example shows how to check file existence using file.exists() method in Java. existspublic boolean exists() Tests whether the file or directory denoted by this abstract pathname exists. Returns:    … Read More
  • How to change File Last modified Date using Java Following example shows how to change file last modified date using file.setLastModified() method in Java. setLastModifiedpublic boolean setLastModified(long time)  Sets the last-modified time of the file or directory … Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

0 comments:

Post a Comment

Item Reviewed: How to append content into existing file in Java Rating: 5 Reviewed By: eHowToNow