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:

 

Hussein Terek

Founder of programmergate.com, I have a passion in software engineering and everything related to java environment.

You may also like...

Leave a Reply

avatar