Java Reference
In-Depth Information
You can also create a File object that represents a pathname for a file by using a constructor that
allows you to specify the directory that contains the file and the file name separately. The directory that
contains the file is usually referred to as the parent directory. There are two constructors that allow you
to do this, providing you with a choice as to how you specify the parent directory. In one, the first
argument is a reference to a File object that encapsulates the path for the directory containing the file.
In the other, the first argument specifies the parent directory path as a String object. The second
argument in both cases is a String object identifying the file name.
For example, on my system, I can identify the Java source file for the File class with the statements:
File myDir = new File("C:/j2sdk1.4.0/src/java/io"); // Parent directory
File myFile = new File(myDir, "File.java"); // The path to the file
The first statement creates a File object that refers to the directory for the package io , and the second
statement creates a File object that corresponds to the file, File.java , in that directory.
We could use the second of the two constructors to achieve the same result as the previous two statements:
File myFile = new File("C:/j2sdk1.4.0/src/java/io", "File.java");
Using a File object to specify the directory is much more useful than using a string directly. For
instance, you can verify that the directory does really exist before attempting to access the file or files
that you are interested in. You can also create the directory if necessary, as we shall see.
The fourth constructor allows you to define a File object from an object of type URI that encapsulates a
U niform R esource I dentifier, commonly known as a URI . As you are undoubtedly aware, a URI is used to
reference a resource on the World Wide Web and the most common form of URI is a URL - a U niform
R esource L ocator. A URL usually consists of a protocol specification such as HTTP , a host machine
identification such as p2p.wrox.com , plus a name that refers to a particular resource on that machine such
as java , so for instance, http://p2p.wrox.com/java is a URL that references a page on a Wrox Press
server that contains a list of forums related to Java topics that are published by Wrox Press.
The URI class provides several constructors for creating URI objects but getting into the detail of these is too
much of a diversion from our present topic - dealing with local files. However, the simplest constructor just
accepts a reference to a String object and we could use this to create a File object like this:
File remoteFile = new File(new URL(http://p2p.wrox.com/java));
References to physical files inevitably tend to be system-dependent since each operating system will
have its own conventions for specifying a path to a file. If you refer to a particular file in the directory
C:\My Java Stuff under Windows, this path will not be recognized under Unix. However, Java
provides capabilities that enable you to avoid system dependencies when you specify file paths, at least
to some extent, so let's look at those in more detail.
Search WWH ::




Custom Search