Java Reference
In-Depth Information
directory itself. The fourth constructor accepts a URI type file specification. The
URI specification encompasses additional ways to specify a file beyond the tradition
file system syntax. (See http://java.sun.com/javase/6/docs/api/java/net/URI.html
for more details.)
You can create a file instance using either full or relative pathnames. If the cur-
rent directory were \documents, you could instantiate a file referencing the my-
books subdirectory in several ways:
// Remember the double slash is required to represent a single slash.
File myDirectory = new File("\\documents\\mybooks");
File myDirectory = new File(".\\mybooks");
File myDirectory = new File("mybooks");
Once the file is instantiated, you can extract information about the file using
various get methods.
Method
Description
getName()
Returns the name of the file or directory without path information.
getPath()
Returns the relative or full path, depending upon how the File was
instantiated.
getParent()
Returns the name of the directory that “owns” the file or directory.
getAbsolutePath()
Returns a nonrelative and absolute pathname that is resolved against
the current drive/directory.
getCanonicalPath()
Returns a nonrelative and absolute pathname that is unique, but highly
system dependent.
The getPath() and getParent() methods return information differently, depend-
ing upon how the File instance was constructed. The getName() , getAbsolutePath() ,
and getCanonicalPath() methods, however, return the same information no matter
how the File instance is constructed. For example, if you construct the mybooks
directory like this:
File myDirectory1 = new File("\\documents\\mybooks");
File myDirectory2 = new File("mybooks");
 
Search WWH ::




Custom Search