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.

Chapter 12. Working with Files<br />

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

You've already learned how to read and write data in files using file input streams and file<br />

output streams. That's not all there is to files. Files can be created, moved, renamed, copied,<br />

deleted, and otherwise manipulated without respect to their contents. Files are also often<br />

associated with meta-information that's not strictly part of the contents of the file, such as the<br />

time the file was created, the icon for the file, the permissions that determine which users can<br />

read or write to the file, and even the name of the file.<br />

While the abstraction of the contents of a file as an ordered sequence of bytes used by file<br />

input and output streams is almost standard across platforms, the meta-information is not. The<br />

java.io.File class attempts to provide a platform-independent abstraction for common file<br />

operations and meta-information. Unfortunately, this class really shows its Unix roots. It<br />

works well on Unix, adequately on Windows and OS/2—with a few caveats—and fails<br />

miserably on the Macintosh. <strong>Java</strong> 2 improves things, but there's still a lot of history—and<br />

coming up with something that genuinely works on all platforms is an extremely difficult<br />

problem.<br />

File manipulation is thus one of the real difficulties of cross-platform <strong>Java</strong> programming.<br />

Before you can hope to write truly cross-platform code, you need a solid understanding of the<br />

filesystem basics on all the target platforms. This chapter tries to cover those basics for the<br />

major platforms that support <strong>Java</strong>—Unix; DOS/Windows 3.x ; Windows 95, 98, and NT;<br />

OS/2; and the Mac—then it shows you how to write your file code so that it's as portable as<br />

possible.<br />

12.1 Understanding Files<br />

As far as a <strong>Java</strong> program knows, a file is a sequential set of bytes stored on a disk like a hard<br />

drive or a CD-ROM. There is a first byte in the file, a second byte, and so on, until the end of<br />

the file. In this way a file is similar to a stream. However, a program can jump around in a<br />

file, reading first one part of a file, then another. This isn't possible with a stream.<br />

Macintosh files are a little different. Mac files are divided into two forks, each of which is<br />

equivalent to a separate file on other platforms. The first part of a Mac file is called the data<br />

fork and contains the text, image data, or other basic information of the file. The second part<br />

of the file is called the resource fork and typically contains localizable strings, pictures, icons,<br />

graphical user interface components like menubars and dialogs, executable code, and more.<br />

On a Macintosh, all the standard java.io classes work exclusively with the data fork.<br />

12.1.1 Filenames<br />

Every file has a name. The format of the filename is determined by the operating system. For<br />

example, in DOS and Windows 3.1, filenames are case-insensitive, (though generally<br />

rendered as all capitals), eight ASCII characters long with a three-letter extension.<br />

README.TXT is a valid DOS filename, but Read me before you run this program or your<br />

hard drive will get trashed is not. All ASCII characters from 32 up (that is, noncontrol<br />

characters), except for the 15 punctuation characters (+=/][":;,?*\|) and the space<br />

character, may be used in filenames. A period may be used only as a separator between the<br />

267

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

Saved successfully!

Ooh no, something went wrong!