How do I create and write data into text file?

Here is a code example for creating text file and put some texts in it. This program will create a file called write.txt.



public class WriteTextFileExample {
    public static void main(String[] args) {
        File file = new File("write.txt");

        try (Writer writer =
                     new BufferedWriter(new FileWriter(file))) {
            String contents = "The quick brown fox" +
                    System.getProperty("line.separator") +
                    "jumps over the lazy dog.";

        } catch (IOException e) {

Wayan Saryada

I am a programmer, a runner, an open water diver and currently living in the island of Bali, Indonesia. If you need help on Java programming you can hire me on Fiverr.

Leave a Comment.