SUNY Geneseo Department of Computer Science


Text Files in Java

{Date}

CSci 141, Fall 2003
Prof. Doug Baldwin

Return to List of Lectures

Previous Lecture


Misc

Exam 1 is this Friday (Oct. 10)

Questions?

Reading files for homework 1

[Text Flows from File to Program Via a Stream and a Reader]

Text differs from arbitrary data

Generic code to get text from a file

  1. Open reader to file
  2. Read strings or characters from reader

Step 1

        FileReader r = new FileReader( "SimpleHunt.txt" );
        BufferedReader in = new BufferedReader( r );

or

        BufferedReader in = new BufferedReader( new FileReader( "SimpleHunt.txt" ) );

Step 2

        String lineOfText = in.readLine();
        RobotRoom room = new RobotRoom( lineOfText );

Directories and Path Names

[A Hierarchy of Directories and Files]

Exceptions

    try {
        ... code that might throw exception
    }
    catch ( Exception e ) {
        ... code to handle exceptions
    }

Next

Recurrence relations

Read Section 7.2.1


Next Lecture