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 />

of its own parents, there's potential for infinite recursion. To avoid this possibility, all paths<br />

are converted to canonical paths in the constructor, and these paths are stored in the static<br />

vector seen. A directory is listed only if it has not yet been traversed by this program.<br />

12.3.7.3 The listFiles() methods<br />

The two list() methods return arrays of strings. The strings contain the names of files. You<br />

can use these to construct File objects. <strong>Java</strong> 2 allows you to eliminate the intermediate step<br />

of creating File objects by providing two listFiles() methods that return arrays of File<br />

objects instead of arrays of strings.<br />

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

public File[] listFiles(FilenameFilter filter) // <strong>Java</strong> 2<br />

public File[] listFiles(FileFilter filter) // <strong>Java</strong> 2<br />

The no-argument variant of listFiles() simply returns an array of all the files in the given<br />

directory. The other two variants return the files that pass through their filters. File and<br />

filename filters will be discussed shortly.<br />

12.3.8 File URLs<br />

File URLs are used inside web browsers to refer to a file on the local hard drive. [3] They have<br />

the basic form:<br />

file:///<br />

should be the fully qualified domain name of the system on which the is<br />

found, though if it's omitted, the local host is assumed. is the hierarchical path to the<br />

file, using a forward slash as a directory separator (regardless of host filename conventions)<br />

and URL encoding of any special characters in filenames that would normally be encoded in a<br />

URL. Examples of file URLs include:<br />

file:///C|/docs/JCE%201.2%20beta%201/guide/API_users_guide.html<br />

file:///usr/local/java/docs/JCE%201.2%20beta%201/guide/API_users_guide.html<br />

file:///D%7C/JAVA/<br />

file:///Macintosh%20HD/<strong>Java</strong>/Cafe%20%au%20%Lait/course/week4/01.5.html<br />

Many web browsers allow other, nonstandard formats like:<br />

file:///D:/JAVA/<br />

file:///usr/local/java/docs/JCE 1.2 beta 1/guide/API_users_guide.html<br />

file:///C|/jdk2beta4/docs/JCE 1.2 beta 1/guide/API_users_guide.html<br />

file:///C:\jdk1.2beta4\docs\JCE 1.2 beta 1\guide\API_users_guide.html<br />

file:/D:/<strong>Java</strong>/ioexamples/12/FileDialogApplet.html<br />

Because of the differences between file, and directory names from one computer to the next,<br />

the exact syntax of file URLs is unpredictable from platform to platform and web browser to<br />

web browser. In <strong>Java</strong> 2 the File class has a toURL() method that returns a file URL that's<br />

appropriate for the local platform:<br />

public URL toURL() throws MalformedURLException // <strong>Java</strong> 2<br />

3 Very early web browsers used file URLs to refer to FTP sites. However, that usage has mostly disappeared now.<br />

298

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

Saved successfully!

Ooh no, something went wrong!