Learn Java Tutorial for Beginners (Video), Part 41: Writing Text Files

A tutorial on writing text files, Java 7 style. If you want Java 6 style, you can easily take the code here and re-arrange it along the lines of the previous tutorial on reading text files in Java. Writing text files requires the same kind of Russian doll approach that reading them does, unfortunately, but at least you can simplify the code a lot using the try-with-resources syntax that Java 7 introduced.

When the video is running, click the maximize button in the lower-right-hand corner to make it full screen. Don't forget to change to HD also if the video appears fuzzy.



Code for this tutorial:

App.java:

 

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;


public class App {

    
    public static void main(String[] args) {
        File file = new File("test.txt");
         
        try (BufferedWriter br = new BufferedWriter(new FileWriter(file))) {
           br.write("This is line one");
           br.newLine();
           br.write("This is line two");
           br.newLine();
           br.write("Last line.");
        } catch (IOException e) {
            System.out.println("Unable to read file " + file.toString());
        }
 

    }

}