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

If you absolutely must use a console in your applet, the following list shows several thirdparty<br />

consoles that work in Internet Explorer. Some provide additional features over the barebones<br />

implementation of Netscape. Of course, URLs can get stale rather quickly. If for some<br />

reason none of these work for you, you can always do what I did to collect them in the first<br />

place: go to http://developer.java.sun.com/developer and search for "console."<br />

Arial Bardin's <strong>Java</strong> Console http://www.cadviewer.com/<strong>Java</strong>Console.html<br />

Jamie Cansdale's <strong>Java</strong> Console and Class<br />

http://www.obsolete.com/people/cansdale/java/java_console/index.htm<br />

Flusher<br />

Frederic Lavigne's Package fr.l2f http://www.l2fprod.com/<br />

1.6.2 System.out<br />

System.out is the first instance of the OutputStream class most programmers encounter. In<br />

fact, it's often encountered before programmers know what a class or an output stream is.<br />

Specifically, System.out is the static out field of the java.lang.System class. It's an<br />

instance of java.io.PrintStream, a subclass of java.io.OutputStream.<br />

System.out corresponds to stdout in Unix or C. Normally, output sent to System.out<br />

appears on the console. As a general rule, the console converts the numeric byte data<br />

System.out sends to it into ASCII or ISO Latin-1 text. Thus, the following lines write the<br />

string "Hello World!" on the console:<br />

byte[] hello = {72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 10,<br />

13};<br />

System.out.write(hello);<br />

1.6.3 System.err<br />

Unix and C programmers are familiar with stderr, which is commonly used for error<br />

messages. stderr is a separate file pointer from stdout, but often means the same thing.<br />

Generally, stderr and stdout both send data to the console, whatever that is. However,<br />

stdout and stderr can be redirected to different places. For instance, output can be<br />

redirected to a file while error messages still appear on the console.<br />

System.err is <strong>Java</strong>'s version of stderr. Like System.out, System.err is an instance of<br />

java.io.PrintStream , a subclass of java.io.OutputStream . System.err is most<br />

commonly used inside the catch clause of a try/catch block like this:<br />

try {<br />

// Do something that may throw an exception.<br />

}<br />

catch (Exception e) {<br />

System.err.println(e);<br />

}<br />

Finished programs shouldn't have much need for System.err, but it is useful while you're<br />

debugging.<br />

29

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

Saved successfully!

Ooh no, something went wrong!