next up previous
Next: Loop schema for writing Up: Unit 09 Previous: Writing text files

Program for writing to a file

The following program creates a text file called test.txt and writes on it the string "some text written on a file".

import java.io.*;

public class WritingOnFile {
  public static void main(String[] args) throws IOException {
    // opening the file for writing
    FileWriter f = new FileWriter("test.txt");
    // creation of the object for writing
    PrintWriter out = new PrintWriter(f);

    // writing text on the file
    out.println("some text written on a file");

    // closing the output channel and the file
    out.close();
    f.close();
  }
}

Notes:


next up previous
Next: Loop schema for writing Up: Unit 09 Previous: Writing text files