Looking to Write String to File Java? Lets explore different ways to achieve it.
- Using BufferedWriter
- With FileOutputStream
- Using PrintWriter
- With DataOutputStream
- Using FileChannel
- Create and Write to a Temporary File
Contents
Using BufferedWriter
Let’s use BufferedWriter of Java to write a String to a new text file.
public static void main(String[] args) throws IOException {
try {
String value = "Welcome";
BufferedWriter bw = new BufferedWriter(new FileWriter("src/resources/hello.txt"));
bw.write(value);
bw.close();
System.out.println("Output written to hello.txt is: " +value);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
Output written to hello.txt is: Welcome
With FileOutputStream
Let’s use FileOutputStream class to write binary data to a text file.
Below code converts a String byte array and writes the bytes to a file using FileOutputStream.
The file path is passed to the output stream to write the desired string value as per the program.
public static void main(String[] args) throws IOException {
try {
String value = "Welcome";
FileOutputStream fos = new FileOutputStream("src/resources/hello.txt");
byte[] bytes = value.getBytes();
fos.write(bytes);
fos.close();
System.out.println("Output written to hello.txt is: " +value);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
Output written to hello.txt is: Welcome
Using PrintWriter
We can use PrintWriter object to write formatted text to a file.
First the file path is passed to the FileWriter class.
And then PrintWriter class will process the Java FileWriter to print the formatted text in a file.
public static void main(String[] args) throws IOException {
try {
FileWriter fileWriter = new FileWriter("src/resources/hello.txt");
PrintWriter pw = new PrintWriter(fileWriter);
pw.printf("Welcome to %s in Year %d", "Java World", 2020);
pw.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
Output written to hello.txt is below
Welcome to Java World in Year 2020
With DataOutputStream
Let’s leverage DataOutputStream to write a String to a file.
Also the writeUTF method takes care of character encoding format that has to be written to a file.
The default character encoding is modified UTF-8 format.
public static void main(String[] args) throws IOException {
try {
String value = "Welcome";
FileOutputStream fs = new FileOutputStream("src/resources/hello.txt");
DataOutputStream os = new DataOutputStream(new BufferedOutputStream(fs));
os.writeUTF(value);
os.close();
System.out.println("Output written to hello.txt is: " +value);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
Output written to hello.txt is: Welcome
Using FileChannel
Let’s use FileChannel to write a String to a file.
Also FileChannel will be faster than the traditional Java IO.
public static void main(String[] args) throws IOException {
try {
RandomAccessFile stream = new RandomAccessFile("src/resources/hello.txt", "rw");
FileChannel channel = stream.getChannel();
String value = "Welcome";
byte[] strBytes = value.getBytes();
ByteBuffer buffer = ByteBuffer.allocate(strBytes.length);
buffer.put(strBytes);
buffer.flip();
channel.write(buffer);
stream.close();
channel.close();
System.out.println("Output written to hello.txt is: " +value);
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
Output written to hello.txt is: Welcome
Create and Write to a Temporary File
Let’s create a temporary file using createTempFile method from File Object.
And then to write a String to a file, lets use Java FileWriter class.
public static void main(String[] args) throws IOException {
try {
String value = "Welcome";
File tempFile = File.createTempFile("testfile", ".tmp");
FileWriter writer = new FileWriter(tempFile);
writer.write(value);
writer.close();
} catch (Exception e) {
e.printStackTrace();
}
}
Output:
The testfile.tmp will be created in Temp folder and the value 'Welcome' written to the temp file.
To conclude this java tutorial, we covered various examples to write string to a file in Java.
Happy Coding Readers 🙂