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.

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

To read a file, just pass the name of the file into the FileInputStream() constructor. Then<br />

use the read() method as normal. For example, the following code fragment reads the file<br />

README.TXT, then prints it on System.out:<br />

try {<br />

FileInputStream fis = new FileInputStream("README.TXT");<br />

int n;<br />

while ((n = fis.available()) > 0) {<br />

byte[] b = new byte[n];<br />

int result = fis.read(b);<br />

if (result == -1) break;<br />

String s = new String(b);<br />

System.out.print(s);<br />

} // End while<br />

} // End try<br />

catch (<strong>IO</strong>Exception e) {System.err.println(e);}<br />

System.out.println();<br />

<strong>Java</strong> looks for files in the current working directory. Generally, this is the directory you were<br />

in when you typed java program_name to start running the program. You can open a file in a<br />

different directory by passing a full or relative path to the file from the current working<br />

directory. For example, to read the file /etc/hosts no matter which directory is current, you can<br />

do this:<br />

FileInputStream fis = new FileInputStream("/etc/hosts");<br />

Notice that this code depends on Unix-style pathnames. It is not guaranteed to work on<br />

Windows or the Mac, though it might; some runtime environments like Apple's Macintosh<br />

Runtime for <strong>Java</strong> include extra code to translate from Unix-style filenames to the native style.<br />

If the file you're trying to read does not exist when the FileInputStream object is<br />

constructed, a FileNotFoundException (a subclass of java.io.<strong>IO</strong>Exception) is thrown. If<br />

for some other reason a file cannot be read—for example, the current process does not have<br />

read permission for the file—some other kind of <strong>IO</strong>Exception is thrown.<br />

Example 4.1 reads filenames from the command line, then copies the named files onto<br />

System.out. The StreamCopier.copy() method from Example 3.3 in the last chapter does<br />

the actual reading and writing. Notice that that method does not care that the input is coming<br />

from a file or going to the console. It works regardless of the type of the input and output<br />

streams it's copying. It will work equally well for other streams still to be introduced,<br />

including ones that did not even exist when StreamCopier was created.<br />

Example 4.1. The FileTyper Program<br />

import java.io.*;<br />

import com.macfaq.io.*;<br />

public class FileTyper {<br />

public static void main(String[] args) {<br />

if (args.length == 0) {<br />

System.err.println("Usage: java FileTyper file1 file2 ...");<br />

return;<br />

}<br />

52

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

Saved successfully!

Ooh no, something went wrong!