Java Reference
In-Depth Information
Now, how can you send the standard output of the type program to the program? Using > will
not work here, as you're not sending the output to a file. Instead, you can use the | operator, called
the pipe , as it pipes the output from one program to another, i.e.: type input.txt | java -jar
linereverser.jar . See Figure 8-11.
fiGure 8-11  
Piping and output redirection are two very powerful features of command-line programs. They
allow for writing simple, one-task-only programs that can then be combined and chained together in
a manner that's more flexible than what most GUI applications can offer. This aspect forms one of
the key reasons why command-line programs remain useful and used in server environments.
Before we end this section, let us return to Eclipse and Java to highlight a more advanced alternative
of the standard streams, named the Console . This is a class that lives under java.io.Console and
can be accessed through System.console() . This object provides all features the standard streams
have, as well as some other features, such as secure input mechanisms (such as for password entry).
The Console also provides input and output streams that are character streams. A downside of this
technique however is that the Console is not available in all operating systems or environments, so
the recommended approach remains to use the standard streams, unless you really need a feature
Console provides. The following Try It Out shows how to read passwords in a secure manner using
the Console .
advanced Command-Line Interaction Using the Console
try it out
In this Try It Out, we will use System.console() to read in a password in a secure manner.
1.
You will create a single class named GetPassword with the following content:
import java.io.Console;
import java.io.IOException;
public class GetPassword {
public static void main (String args[]) throws IOException {
Console c = System.console();
if (c == null) {
System.err.println("Console object is not available");
System.exit(1);
}
String username = c.readLine("Enter your username: ");
char[] password = c.readPassword("Enter your password: ");
Search WWH ::




Custom Search