Java Reference
In-Depth Information
Portable Path Considerations
The File class contains a static member, separator , of type String that defines the separator that is
used between names in a path by your operating system. Under Unix, this will be defined as " / " and
under MS Windows it will be " \\ ". As we have seen, when you are specifying a path by a string under
Windows, you can use either of these characters as a path name separator. Another static field,
separatorChar , defines the same separator character as type char for convenience, so you can use
whichever suits you. The File class also defines the system default character for separating one path
from another as the static member pathSeparator , which is of type String , or
pathSeparatorChar , which is of type char . The path separator is a semicolon under MS Windows
and a colon under Unix.
Of course, the specific makeup of a path is system-dependent but we could have used the separator field
in the File class to specify the path for myFile in a slightly more system-independent way, like this:
File myFile = new File("C:" + File.separator + "jdk1.4" + File.separator +
"src" + File.separator + "java" + File.separator +
"io", "File.java");
This defines the same path as the previous statement but without using an explicit separator character
between the names in the path. While we have specified the pathname separator character in a portable
fashion, the argument to the File class constructor is still specific to Windows because we have
specified the drive letter as part of the path. To remove the Windows-specific element in the file path
we would have to omit the drive letter from the path specification. In this case we would have a relative
path specification.
Absolute and Relative Paths
In general, the pathname that you use to create a File object has two parts: an optional prefix,
followed by a series of names separated by the system default separator character for pathnames. Under
MS Windows the prefix for a path on a local drive will be a string defining the drive, such as " C:\\ " or
" C:/ ". Under Unix the prefix will be a forward slash, " / ". A path that includes a prefix is an absolute
path and since it includes a prefix it is not system-independent. A path without a prefix is a relative path
and as long as it consists of one or more names separated by characters specified as File.separator
or File.separatorChar it should be portable across different systems. The last name in a path can
be a directory name or a filename. All other names must be directory names. If you use anything other
than this - if you use any system-specific conventions to specify the path for instance - naturally you no
longer have a system-independent path specification.
The pathnames we have used in the preceding code fragments have all been absolute paths, since we
included the drive letter in the path for Windows, or a forward slash to identify the Unix root directory.
If you omit this, you have a relative path, and the pathname string will be interpreted as a path relative
to the current directory. This implies that you can reference a file that is in the same directory as your
program by just the file name.
For example:
File myFile = new File("output.txt");
Search WWH ::




Custom Search