How to write to a plain text file in Java

This tutorial shows several ways to write content to a text file in Java.

The techniques used below are pure JDK and don’t use external libraries.

1- BufferedWriter

The most common and efficient way to write content to a file in Java is through using BufferedWriter as the following:

This technique is very efficient for writing huge data to a text file.

2- Java nio

Using nio package which is introduced in JDK 7, you can write content to a text file in one line using Files.write() as the following:


0 0 votes
Article Rating

Hussein Terek

Owner of, I have a passion for software engineering and everything related to Java environment.

Inline Feedbacks
View all comments