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.

File temp = new File("com");<br />

temp = new File(temp, "macfaq");<br />

temp = new File(temp, "io");<br />

File scfile = new File(temp, "StreamCopier.class");<br />

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

None of these constructors throw any exceptions. All the constructor does is set the path<br />

field; <strong>Java</strong> never checks to see whether the file named by path actually exists or even whether<br />

the name passed to the constructor is a valid filename. For example, the following File object<br />

will cause problems on Unix, OS/2, the Mac, and Windows; but you can still construct it:<br />

File f = new File("-This is not a /nice\\ file:\r\nno it isn't");<br />

Some methods in other classes also return File objects, most notably the<br />

java.awt.FileDialog and javax.awt.swing.JFileChooser methods discussed in the next<br />

chapter. Using file dialogs or choosers to ask the user for a filename is preferable to<br />

hardcoding them or reading them from the command line, because file dialogs properly<br />

handle cross-platform issues and the distinctions between relative and absolute paths.<br />

One thing you may not have noticed about these constructors: since a<br />

File object does not represent a file as much as a filename, these<br />

constructors do not actually create files. To create a new file with <strong>Java</strong>,<br />

you can open a file output stream to the file or invoke the<br />

createNewFile() method. The latter only works in <strong>Java</strong> 2 and later.<br />

In <strong>Java</strong> 2 and later, construction of a File object includes normalization. This process reads<br />

hardcoded pathnames and attempts to convert them to the conventions of the local platform.<br />

This improves compatibility with code that's making assumptions about filenames. For<br />

instance, if a Windows VM is asked to create a File object with the path<br />

/public/html/javafaq/course/week2/index.html, it will actually set the path field to \ public\<br />

html\ javafaq\ course\ week2\ index.html. The reverse process happens on Unix; backslashes<br />

are converted to forward slashes. <strong>Java</strong> 2 is not available for the Mac at the time of this<br />

writing, so it remains to be seen how the Mac will normalize filenames. Because it can only<br />

really normalize separators, not filesystem roots, this scheme works better for relative<br />

pathnames than absolute ones.<br />

12.3.2 Listing the Roots<br />

<strong>Java</strong> 2 provides a static File.listRoots() method that returns an array of all the available<br />

roots of the filesystem as File objects:<br />

public static File[] listRoots() // <strong>Java</strong> 2<br />

On Unix, this array is likely to have length 1 and contain the single root /. On Windows, it<br />

will probably contain all the drive letters mapped to one device or another, whether or not<br />

there's actually any media in the drive; e.g., A:\, C:\, D:\, E:\, F:\, G:\. On the Mac, the list<br />

will likely contain only the currently mounted drives, and these are likely to have somewhat<br />

more descriptive names than C:\. If the security manager does not allow the program to read<br />

a particular root, then that root is not included in the returned list. If the security manager does<br />

not allow the program to read any root, then the returned list will have length zero. Do not<br />

282

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

Saved successfully!

Ooh no, something went wrong!