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.

1.6.4 System.in<br />

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

System.in is the input stream connected to the console, much as System.out is the output<br />

stream connected to the console. In Unix or C terms, System.in is stdin and can be<br />

redirected from a shell in the same fashion. System.in is the static in field of the<br />

java.lang.System class. It's an instance of java.io.InputStream, at least as far as is<br />

documented.<br />

Past what's documented, System.in is really a java.io.BufferedInputStream.<br />

BufferedInputStream doesn't declare any new methods, just overrides the ones already<br />

declared in java.io.InputStream. Buffered input streams read data in large chunks into a<br />

buffer, then parcel it out in requested sizes. This can be more efficient than reading one<br />

character at a time. Otherwise, it's completely transparent to the programmer.<br />

The main significance of this is that each byte is not presented to be read as the user types it<br />

on System.in. Instead, input enters the program one line at a time. This allows a user typing<br />

into the console to backspace over and correct mistakes. <strong>Java</strong> does not allow you to put the<br />

console into "raw mode," where each character becomes available as soon as it's typed,<br />

including characters like backspace and delete.<br />

In an application run from the command line, System.in is taken from the window where the<br />

program was started; that is, the console. In applets, the same console window that's used for<br />

System.out is also used for System.in ; however, Internet Explorer has no way to read from<br />

System.in in an applet. In Netscape, the console is turned off by default, and users must<br />

explicitly request that it be turned on.<br />

The user types into the console using the platform's default character set, typically ASCII or<br />

some superset thereof. The data is converted into numeric bytes when read. For example, if<br />

the user types "Hello World!" and hits the return or enter key, the following bytes will be read<br />

from System.in in this order:<br />

72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33, 10, 13<br />

Many programs that run from the command line and read input from System.in require you<br />

to enter the "end of stream" character, also known as the "end of file" or EOF character, to<br />

terminate a program normally. How this is entered is platform-dependent. On Unix and the<br />

Mac, Ctrl-D generally indicates end of stream. On Windows, Ctrl-Z does. In some cases it<br />

may be necessary to type this character alone on a line. That is, you may need to hit<br />

Return/Ctrl-Z or Return/Ctrl-D before <strong>Java</strong> will recognize the end of stream.<br />

1.6.5 Redirecting System.out, System.in, and System.err<br />

In a shell you often redirect stdout, stdin, or stderr. For example, to specify that output<br />

from the <strong>Java</strong> program OptimumBattingOrder goes into the file yankees99.out and that input<br />

for that program is read from the file yankees99.tab, you might type:<br />

% java OptimumBattingOrder < yankees99.tab > yankees99.out<br />

Redirection in a DOS shell is the same. It's a little more complicated in graphical<br />

environments, but not particularly difficult. To give one example, the JBindery tool included<br />

30

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

Saved successfully!

Ooh no, something went wrong!