Java Reference
In-Depth Information
interface is still present under the hood. In Windows, for instance, you can try firing up the cmd.exe
program to get the command prompt shown in Figure 8-4.
fiGure 8-4
When you execute Java programs in Eclipse, they will also show their output and take their input
from a command-line interface, unless you program in some kind of GUI features. We refer here to
the Eclipse console. Refer back to Chapter 3 if you want to know how you can run Java programs
from the common Windows command-line.
In every console application, three mechanisms exist to communicate with the user. In Java, these
are implemented as the “standard streams,” corresponding to the following:
System.in : A byte InputStream to take user input (you might have expected this to be a
character stream, but for historical reasons, this is not the case)
System.err : A PrintStream to output error messages
System.out : A PrintStream to output normal messages
You can use these streams just as you would use normal Java streams, with the difference, however,
that you don't need to open or close them. The following Try It Out shows how it works.
Input and Output from the Command-Line
try it out
This Try It Out illustrates the basic use of System.in , System.err , and System.out by means of a
simple greeter application.
1.
Create a single class named ReadName with the following content:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ReadName {
public static void main(String[] args) {
try(
BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in));
) {
System.out.println("What is your name, user?");
String name = reader.readLine();
Search WWH ::




Custom Search