Different ways to append text to a file in Java:
In this post, we will learn different ways to append text to a file in Java. Appending content to a file will not modify the existing content. It will open the file, add text to the end of it and close the file.
We can do that in different ways in Java. Let’s take a look at these methods one by one:
Method 1: By using FileOutputStream:
FileOutputStream is an output stream for writing data to a file. The second argument of FileOutputStream is a boolean value. It should be true to append data to a stream.
Below is the complete program:
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
class Main {
public static void main(String[] args) {
String fileName = "readme.md";
String data = "Hello World !!";
FileOutputStream outputStream = null;
try {
outputStream = new FileOutputStream(fileName, true);
outputStream.write(data.getBytes());
} catch (IOException e) {
e.printStackTrace();
} finally {
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
It will append data to the file fileName. It creates the file if it doesn’t exist.
Method 2: Append to file using BufferedWriter:
We need to create one FileWriter object and we can create one BufferedWriter object with this FileWriter. BufferedWriter provides one method called write to write data.
Complete program:
import java.io.*;
class Main {
public static void main(String[] args) {
String fileName = "readme.md";
String data = "Hello World !!";
FileWriter fileWriter = null;
BufferedWriter bufferedWriter;
File file = new File(fileName);
try {
fileWriter = new FileWriter(file, true);
bufferedWriter = new BufferedWriter(fileWriter);
bufferedWriter.write(data);
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileWriter != null)
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
We are using the same variables as the previous program. It will create the file fileName if it doesn’t exist.
Here, we need to pass true as the second parameter to FileWriter to make it append operation.
Method 3: Append to file using PrintWriter:
PrintWriter object can be created by passing one BufferedWriter as its argument. We can use print method of PrintWriter to append a string to the file. We can also use println to add one new line to the end.
import java.io.*;
class Main {
public static void main(String[] args) {
String fileName = "readme.md";
String data = "Hello World !!";
FileWriter fileWriter = null;
BufferedWriter bufferedWriter = null;
PrintWriter printWriter = null;
File file = new File(fileName);
try {
fileWriter = new FileWriter(file, true);
bufferedWriter = new BufferedWriter(fileWriter);
printWriter = new PrintWriter(bufferedWriter);
printWriter.print(data);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (printWriter != null)
printWriter.close();
try {
if (fileWriter != null)
fileWriter.close();
if (bufferedWriter != null)
bufferedWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
This will create the file if it doesn’t exist.
Method 4: Append data by using FileWriter:
FileWriter class provides one method called write that can be used to write a string to a file. We need to pass the second argument as true to FileWriter.
import java.io.*;
class Main {
public static void main(String[] args) {
String fileName = "readme.md";
String data = "Hello World !!";
FileWriter fileWriter = null;
File file = new File(fileName);
try {
fileWriter = new FileWriter(file, true);
fileWriter.write(data);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileWriter != null) {
try {
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
It creates the file if it doesn’t exist.
Method 5: Using Files:
Files class is started in Java-7. It makes the process easier. Below program uses write method of Files class to append a string:
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.NoSuchFileException;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
class Main {
public static void main(String[] args) {
String fileName = "readme.md";
String data = "Hello World !!";
try {
Files.write(Paths.get(fileName), data.getBytes(), StandardOpenOption.APPEND);
} catch (NoSuchFileException e){
System.out.println("File doesn't exist");
} catch (IOException e) {
e.printStackTrace();
}
}
}
Note that Files class will throw one Exception if the file doesn’t exist.
You might also like:
- Java program to find the sum of the series 1 + 1/2 + 1/3 + 1/4 or harmonic series
- Java program to print the Arithmetic Progression series
- How to reverse a string in Java
- Java example program to find the area of a Parallelogram
- Write a number guessing game in Java
- How to overload main method in Java
- Java program to find the perimeter of a circle