Java Reference
In-Depth Information
Display 10.9
Checking for the End of a Text File with
BufferedReader
(part 2 of 2)
33 System.out.println("Problem opening files.");
34 }
35
catch
(IOException e)
36 {
37 System.out.println("Error reading from original.txt.");
38 }
39 }
40 }
F
ILE
original.txt
Little Miss Muffet
sat on a tuffet
eating her curves away.
Along came a spider
who sat down beside her
and said "Will you marry me?"
F
ILE
numbered.txt
(after the program is run)
1 Little Miss Muffet
2 sat on a tuffet
3 eating her curves away.
4 Along came a spider
5 who sat down beside her
6 and said "Will you marry me?"
If your version of
numbered.txt
has
numbered blank lines after
6
, that means
you had blank lines at the end of
original.txt
.
Self-Test Exercises
17. Does the class
BufferedReader
have a method to read an
int
value from a text file?
18. What happens when the method
readLine
in the class
BufferedReader
attempts to read beyond the end of a file? How can you use this to test for the
end of a file?
19. What is the type of the value returned by the method
read
in the class
Buffered-
Reader
?
20. What happens when the method
read
in the class
BufferedReader
attempts to
read beyond the end of a file? How can you use this to test for the end of a file?
21. Does the program in Display 10.9 work correctly if
original.txt
is an empty file?
Path Names
When giving a file name as an argument to a constructor for opening a file in any of
the ways we have discussed, you may use a simple file name, in which case it is