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.

12.1.3 Filename Extensions and File Types<br />

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

Filename extensions are often used to indicate the type of a file. For example, a file that ends<br />

with the four-letter extension .java is presumed to be a text file containing <strong>Java</strong> source code; a<br />

file ending in the five-letter extension .class is assumed to contain compiled <strong>Java</strong> byte code; a<br />

file ending in the three-letter extension .gif is assumed to contain a GIF image. Table 12.1 lists<br />

some of the more common extensions and their associated types.<br />

Table 12.1. Extension Type Mappings<br />

Extension Type Extension Type<br />

.txt ASCII text .sit StuffIt archive<br />

.gif GIF image .bin MacBinary file<br />

.jpg, .jpeg JPEG image .hqx BinHexed Macintosh file<br />

.htm, .html HTML text .tar Unix tar archive<br />

.java <strong>Java</strong> source code .doc Microsoft Word file<br />

.class compiled <strong>Java</strong> class .c C source code<br />

.jar JAR archive .pl Perl program<br />

.zip Zip archive .cc, .cpp C++ source code<br />

.Z Unix compressed file .o Prelinked native object code<br />

.gz gzipped file .exe DOS/Windows executable<br />

What does your computer do when you double-click on the file panther.gif? If your computer<br />

is a Macintosh, it opens the file in the program that created the file. That's because the MacOS<br />

stores a four-letter creator code for every file on a disk in the disk's volume catalog. Assuming<br />

the application associated with that creator code can be found (it can't always, though), the<br />

file panther.gif is opened in the creating program. On the other hand, if your computer is a<br />

Windows PC or a Unix workstation, the creating program is not necessarily opened. Instead,<br />

whichever program is registered as the viewer of .gif files is launched and used to view the<br />

file. In command-line environments, like the Unix shell, this isn't really an issue, because you<br />

begin by specifying the program to run; that is, you type xv panther.gif, not simply<br />

panther.gif. But in GUI environments, the program that's opened may not be the program you<br />

want to use.<br />

File extensions have the further disadvantage that they do not really guarantee the content<br />

type of their document and are an unreliable means of determining the type of a file. Users<br />

can easily change them. For example, the simple command copy HelloWorld.java<br />

HelloWorld.gif causes an ASCII text file to be misinterpreted as a GIF image. Filename<br />

extensions are only as reliable as the user that assigned them. What's more, it's hard to<br />

distinguish between files that belong to different applications that have the same type. For<br />

instance, many users are surprised to discover that after installing Internet Explorer, all their<br />

HTML files appear to belong to Explorer instead of Netscape.<br />

The Macintosh solved this problem over a decade ago. Almost every Mac file has a four-letter<br />

type code like "TEXT" and a four-letter creator code like "R*ch". Since each file has both a<br />

type code and a creator code, a Mac can distinguish between files that belong to different<br />

applications but have the same type. Installing Internet Explorer doesn't mean that IE<br />

suddenly thinks it owns all your Netscape documents, as is the case when you install IE on<br />

Windows. Software vendors register codes with Apple so companies don't accidentally step<br />

on each other's toes; and since codes are almost never seen by end users, there's not a huge<br />

272

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

Saved successfully!

Ooh no, something went wrong!