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)
- Basics of design, theory, experimentation, and recursion, induction, recurrences
- Open book, open notes, open computer
- 3 - 5 short-answer questions (algorithms, proofs, paragraphs, etc.)
- Whole class period
- Sample test will be on Web
Questions?
Reading files for homework 1
![[Text Flows from File to Program Via a Stream and a Reader]](stream.gif)
Text differs from arbitrary data
- Division into lines
- Contents are always characters
Generic code to get text from a file
- Open reader to file
- 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]](filesystem.gif)
- Project builder: current directory when program runs is "build"
directory inside the project directory
- Path name: series of directories from root to what you want
- e.g., /Users/LabUser/myFile
Exceptions
- Bad things sometimes happen to good programs
- "Bad thing" "throws" an exception
- Causes control transfer out of part of program that detects error, to some
other part - "Exception handler"
try {
... code that might throw exception
}
catch ( Exception e ) {
... code to handle exceptions
}
Next
Recurrence relations
Read Section 7.2.1
Next Lecture