code.dreamsense.com

March 21, 2020

Last time we talked about reading text files, today we're talking about writing text files.

Text files are the most simplest file to have. It's plain text. there's no fancy document things in it. No tables, no bolded elements, nothing. Just plain text.

So, how do we best write a text file? Like the following:

  import java.io.FileWriter;
  import java.io.IOException;

  public class WriteFile {

    public static void main(String[] args) {
      try {
        FileWriter myWriter = new FileWriter("myfile.txt");
        myWriter.write("Hello World!");
        myWriter.close();
      } catch (IOException e) {
        System.out.println("An error occurred.");
        e.printStackTrace();
      }
    }
  }

Kyle Eggleston
kyle@dreamsense.com
Copyright © 2003 - 2020 archive