How to write line by line to a text file using Java code

Hi guys! In this tutorial I will explain how you can write to a text file using Java code.

At the top of my java file I will add this code:


Next, I will open my ab.text file by using File newfile = new File("ab.txt");

Next, I will add this code:

FileWriter writer = new FileWriter(newfile.getAbsoluteFile());
BufferedWriter write = new BufferedWriter(writer);
write.write("Hello World!");

If you like to write line by line, you can use this code:

for (int i = 0; i < 10; i++) { write.write("Hello World!"); write.newLine(); } instead write.write("Hello World!"); Video tutorial:

Leave a Comment