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.

catch (<strong>IO</strong>Exception e) {<br />

System.out.println("Could not determine the canonical path.");<br />

}<br />

String parent = f.getParent();<br />

if (parent != null) {<br />

System.out.println("Parent: " + f.getParent());<br />

}<br />

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

if (f.canWrite()) System.out.println(f.getName() + " is<br />

writable.");<br />

if (f.canRead()) System.out.println(f.getName() + " is readable.");<br />

if (f.isFile()) {<br />

System.out.println(f.getName() + " is a file.");<br />

}<br />

else if (f.isDirectory()) {<br />

System.out.println(f.getName() + " is a directory.");<br />

}<br />

else {<br />

System.out.println("What is this?");<br />

}<br />

if (f.isAbsolute()) {<br />

System.out.println(f.getPath() + " is an absolute path.");<br />

}<br />

else {<br />

System.out.println(f.getPath() + " is not an absolute path.");<br />

}<br />

long lm = f.lastModified();<br />

if (lm != 0) System.out.println("Last Modified at " + new<br />

Date(lm));<br />

long length = f.length();<br />

if (length != 0) {<br />

System.out.println(f.getName() + " is " + length + " bytes<br />

long.");<br />

}<br />

}<br />

else {<br />

System.out.println("I'm sorry. I can't find the file " + args[i]);<br />

}<br />

}<br />

}<br />

}<br />

Here's the result of running FileSpy on itself:<br />

D:\JAVA\ioexamples\12>java FileSpy FileSpy.java<br />

Name: FileSpy.java<br />

Absolute path: D:\JAVA\ioexamples\12\FileSpy.java<br />

Canonical path: D:\<strong>Java</strong>\ioexamples\12\FileSpy.java<br />

FileSpy.java is writable.<br />

FileSpy.java is readable.<br />

FileSpy.java is a file.<br />

FileSpy.java is not an absolute path.<br />

Last Modified at Fri Sep 11 15:11:24 PDT 1998<br />

FileSpy.java is 1846 bytes long.<br />

290

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

Saved successfully!

Ooh no, something went wrong!