Java Reference
In-Depth Information
Display 10.9
Checking for the End of a Text File with
BufferedReader
(part 2 of 2)
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 line
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 fi le?
18. What happens when the method
readLine
in the class
BufferedReader
attempts to read beyond the end of a fi le? How can you use this to test for the
end of a fi le?
19. What is the type of the value returned by the method
read
in the class
BufferedReader
?
20. What happens when the method
read
in the class
BufferedReader
attempts to
read beyond the end of a fi le? How can you use this to test for the end of a fi le?
21. Does the program in Display 10.9 work correctly if
original.txt
is an empty fi le?
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 assumed
that the file is in the same directory (folder) as the one in which the program is run.
You can also use a full or relative path name.
A
path name
not only gives the name of the file, but also tells what directory
(folder) the file is in. A
full path name
, as the name suggests, gives a complete path
name, starting from the root directory. A
relative path name
gives the path to the
path names