Java Reference
In-Depth Information
Solution
Use a java.io.File object.
Discussion
The File class has a number of “informational” methods. To use any of these, you must con-
struct a File object containing the name of the file it is to operate upon. It should be noted
up front that creating a File object has no effect on the permanent filesystem; it is only an
object in Java's memory. You must call methods on the File object in order to change the
filesystem; as we'll see, there are numerous “change” methods, such as one for creating a
new (but empty) file, one for renaming a file, etc., as well as many informational methods.
Table 11-1 lists some of the informational methods.
Table 11-1. java.io.File methods
Return type Method name
Meaning
True if something of that name exists
boolean
exists()
getCanonicalPath() Full name
String
Relative filename
String
getName()
Parent directory
String
getParent()
True if file is readable
boolean
canRead()
True if file is writable
boolean
canWrite()
File modification time
long
lastModified()
File size
long
length()
True if it's a file
boolean
isFile()
True if it's a directory (note: it might be neither)
You can't change the name stored in a File object; you simply create a new File object
each time you need to refer to a different file:
Search WWH ::




Custom Search