next up previous
Next: Loop schema for reading Up: Unit 09 Previous: Reading from a text

Program for reading from a file

The following program opens a text file called test.txt, reads from it a line of text, and prints in on the video.

import java.io.*;

public class ReadingFromFile {
  public static void main(String[] args) throws IOException {
    // opening the file for reading
    FileReader f = new FileReader("test.txt");
    // creation of the object for reading
    BufferedReader in = new BufferedReader(f);

    // reading a line of text from the file
    String line = in.readLine();
    System.out.println(line);

    // closing the file
    f.close();
  }
}

Notes:


next up previous
Next: Loop schema for reading Up: Unit 09 Previous: Reading from a text