Quantcast
Channel: Tech Tutorials
Viewing all articles
Browse latest Browse all 938

How to append to a file in Java

$
0
0

In the post writing file in Java we have already seen how to write to a file in Java but the code given there creates a new file every time and writes the lines in the file. But there are many cases when you actually want to append to the already existing file.

In this post we’ll see how to append to a file in Java. Both FileOutputStream and FileWriter classes have a constructor with a boolean argument, which when passed as true, means appending to a file. Here FileOutPutStream and FileWriter are classes provided by Java to write files using byte stream and character stream respectively but using them directly will slow down the I/O operation considerably.

It is always advisable to use BufferedOutputStream or BufferedWriter because that will provide buffering to the output streams and won't cause a call to the underlying system for each byte written. Buffered output streams write data to a buffer, and the native output API is called only when the buffer is full, making I/O operation more efficient.

Appending to a file using BufferedOutputStream

In the code if the file is not existing already it will be created and lines will be written to it. If it already exists then lines will be appended. You can run this program twice to see that.


import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileAppend {

public static void main(String[] args) {
// Change path for windows
writeFile("/home/netjs/Documents/text.txt");

}
/**
*
* @param fileName
*/
public static void writeFile(String fileName){
FileOutputStream fos;
BufferedOutputStream bos = null;
try {
fos = new FileOutputStream(fileName, true);
bos = new BufferedOutputStream(fos);
// For windows you may need /r/n for new line
bos.write("Writing first line\n".getBytes());
bos.write("Writing second line\n".getBytes());
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
if(bos != null){
bos.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}

Appending to a file using BufferedWriter

In the code if the file is not existing already it will be created and lines will be written to it. If it already exists then lines will be appended. You can run this program twice to see that.


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

public class FileAppendWrite {

public static void main(String[] args) {
//Change path for windows
writeFile("/home/netjs/Documents/test.txt");
}
/**
*
* @param fileName
*/
public static void writeFile(String fileName){
// Using Java 7 try-with-resources
try (BufferedWriter bw = new BufferedWriter(new FileWriter(fileName, true))){
bw.write("Writing first line");
bw.newLine();
bw.write("Writing second line");
bw.newLine();
}
catch (FileNotFoundException e) {
e.printStackTrace();
}catch (IOException e) {
e.printStackTrace();
}
}
}

That's all for this topic How to append to a file in Java. If you have any doubt or any suggestions to make please drop a comment. Thanks!


Related Topics

  1. Writing file in Java
  2. How to read file from the last line in Java
  3. Reading file in Java using BufferedReader
  4. Zipping files in Java
  5. How to create PDF from XML using Apache FOP
  6. try-with-resources in Java 7

You may also like -

>>>Go to Java Programs page


Viewing all articles
Browse latest Browse all 938

Trending Articles