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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

There are also two unrelated mnemonic constants, File.pathSeparator and<br />

File.pathSeparatorChar. The path separator string is set from the system property<br />

path.separator. As with the separator character, File.pathSeparatorChar is the first<br />

character in File.pathSeparator.<br />

public static final String pathSeparator =<br />

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

public static final char pathSeparatorChar = pathSeparator.charAt(0);<br />

The path separator is used to separate two files (generally with complete pathnames) in a list<br />

of paths such as a class path. For example, with a separator of a slash and a path separator of a<br />

colon, my class path looks like this:<br />

.:/usr/local/java/lib:/home/users/elharo/:/home/users/elharo/<strong>Java</strong>Dis/<br />

These four mnemonic constants are one of the few instances in the core<br />

API where <strong>Java</strong>Soft violates its convention of using all caps for public<br />

final static fields. You'd expect these constants to be named<br />

File.SEPARATOR, File.SEPARATOR_CHAR, File.PATH_SEPARATOR, and<br />

File.PATH_SEPARATOR_CHAR. Perhaps a future version of <strong>Java</strong> will<br />

conform to the standard naming conventions.<br />

Now the bad news: although <strong>Java</strong>Soft has provided a fairly powerful abstraction layer so that<br />

programmers don't need to hardcode explicit separators and path separators, few programmers<br />

actually use this. Many programmers simply assume that the file separator is a slash and the<br />

path separator is a colon and hardcode those constants as "/" and ":" or '/' and ':'.<br />

Therefore, to avoid breaking all this third-party code, <strong>Java</strong> 1.1 VMs on the Mac and Windows<br />

generally use a slash for the separator and a colon for the path separator, then make the<br />

appropriate conversions in the native code that underlies <strong>Java</strong>. <strong>Java</strong> 2 VMs pass pathnames<br />

through a normalization phase that attempts to recognize the separator conventions and<br />

convert those to the conventions of the local platform.<br />

This isn't a big problem for Windows, since both the slash and the backslash are illegal<br />

characters in filenames. However, on the Mac a filename can contain both a slash and a<br />

backslash. Macintosh virtual machines have adopted a number of different and incompatible<br />

schemes to distinguish between slashes that are part of filenames and slashes that represent<br />

separators. One early scheme was to replace slashes by colons and colons by slashes.<br />

However, all but one of the alternative Mac VM vendors have canceled their own VM efforts<br />

in favor of Apple's own Macintosh Runtime for <strong>Java</strong> (MRJ), and thus the scheme that MRJ<br />

uses seems likely to become the standard way to handle unusual filenames on the Mac.<br />

Apple's translation scheme is loosely based on the x-www-form-urlencoded format for<br />

encoding URLs. Any characters that are likely to cause problems in a filename are replaced<br />

by a percent sign (%) followed by the two hexadecimal digits corresponding to the ASCII<br />

value of that character. For example, the space is ASCII 32 (decimal) or 20 (hexadecimal).<br />

Thus, it's encoded as %20. The forward slash is ASCII 47 (decimal) or 2f (hexadecimal).<br />

Thus, it's encoded as %2f. The pathname for this chapter, Macintosh HD:<strong>Java</strong>:<strong>Java</strong> I/O:12<br />

Working with Files/12 Working with Files.doc, would be encoded as<br />

/Macintosh%20HD/<strong>Java</strong>/<strong>Java</strong>%20I%2fO/<br />

12%20Working%20with%20Files/12%20Working%20with%20Files.doc. Table 12.2 gives a<br />

276

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

Saved successfully!

Ooh no, something went wrong!