23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

<strong>Java</strong> I/O<br />

one x-type referring to the same basic type (for instance, "application/x-pict" and<br />

"application/x-macpict" both refer to Macintosh PICT images), and the same x-type may be<br />

used for different purposes by different programs. Nonetheless, since new MIME types are<br />

adopted rather slowly, the x-types are important as well.<br />

12.2 Directories and Paths<br />

Modern operating systems organize files into hierarchical directories. Each directory contains<br />

zero or more files or other directories. Like files, directories have names and attributes,<br />

though—depending on the operating system—those names and attributes may be different<br />

from the attributes allowed for files. For example on the Macintosh, a file or directory name<br />

can be up to 31 bytes long, but a volume name can be no more than 27 bytes long.<br />

12.2.1 Paths and Separators<br />

To specify a file completely, you don't just give its name. You also give the directory the file<br />

lives in. Of course, that directory may itself be inside another directory, which may be in<br />

another directory, until you reach the root of the filesystem. The complete list of directories<br />

from the root to a specified file plus the name of the file itself is called the absolute path to<br />

the file. The exact syntax of absolute paths varies from system to system. Here are a few<br />

examples:<br />

DOS C:\PUBLIC\HTML\JAVAFAQ\INDEX.HTM<br />

Win32 C:\public\html\javafaq\index.html<br />

MacOS Macintosh HD:public:html:javafaq:index.html<br />

Unix /public/html/javafaq/index.html<br />

All three strings reference a file named index.html on the primary hard drive in the javafaq<br />

directory, which is itself in the html directory, which is in the public directory. One obvious<br />

difference is the file separator character. Unix uses a forward slash (/) to separate directories;<br />

DOS-based filesystems, including the variants of Windows and OS/2, use a backslash ( \ );<br />

Macs use a colon (:). Other platforms may use something completely different.<br />

The separator used on a given system is available from the mnemonic constants<br />

java.io.File.separator and java.io.File.separatorChar. File.separatorChar is<br />

the first character of the string File.separator. All operating systems I'm familiar with use<br />

a single character separator string, so these two variables are essentially the same. The<br />

File.separator variable is set from the system property file.separator; that is:<br />

public static final String separator =<br />

System.getProperty("file.separator");<br />

public static final char separatorChar = separator.charAt(0);<br />

274

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!