10.3 Techniques for Any File
An ounce of prevention is worth a pound of cure.
—COMMON SAYING
This section discusses some techniques that we can use with both text files and binary files, even though our examples here will involve text files. We begin by describing the standard class File
, which we used in the previous section when reading from a text file.
The Class File
The class File
provides a way to represent file names in a general way. A string such as "treasure.txt"
might be a file name, but it has only string properties, and Java does not recognize it as a file name. On the other hand, if you pass a file name as a string to the constructor of the class File
, it produces an object that can be thought of as the name of a file. In other ...
Get Java: An Introduction to Problem Solving and Programming, 8th Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.