Bash字符串处理(与Java对照) - 5.字符串输入(读取字符串)(一)

2014-11-24 02:38:33 · 作者: · 浏览: 4

In Java
Scanner类:Scanner.hasNext() & Scanner.next() & Scanner.hasNextLine() & Scanner.nextLine()
JavaDoc class Scanner 写道
A simple text scanner which can parse primitive types and strings using regular expressions.

A Scanner breaks its input into tokens using a delimiter pattern, which by default matches whitespace. The resulting tokens may then be converted into values of different types using the various next methods.

A scanning operation may block waiting for input.

The next() and hasNext() methods and their primitive-type companion methods (such as nextInt() and hasNextInt()) first skip any input that matches the delimiter pattern, and then attempt to return the next token. Both hasNext and next methods may block waiting for further input. Whether a hasNext method blocks has no connection to whether or not its associated next method will block.

下面是关于 Scanner.hasNext() 和 Scanner.next() 的例子,用于读取以空白分隔的标识符。
Java代码
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String msg = scanner.next();
System.out.println(msg);
}

下面是关于 Scanner.hasNextLine() 和 Scanner.nextLine() 的例子,用于读取输入数据行。
Java代码
Scanner scanner = new Scanner(System.in);
while (scanner.hasNextLine()) {
String msg = scanner.nextLine();
System.out.println(msg);
}

更详细的说明请参考:http://download.oracle.com/javase/1,5.0/docs/api/java/util/Scanner.html

Console类:System.console() & Console.readLine() & Console.readPassword()
Console类是JDK6加入的,它是对控制台的封装。使用 System.console() 可以获得与当前Java虚拟机关联的控制台,如果有的话。Console.readLine可以读取一行,Console.readPassword可以读取密码。

JavaDoc class Console 写道
public final class Console
extends Object
implements Flushable

Methods to access the character-based console device, if any, associated with the current Java virtual machine.

Whether a virtual machine has a console is dependent upon the underlying platform and also upon the manner in which the virtual machine is invoked. If the virtual machine is started from an interactive command line without redirecting the standard input and output streams then its console will exist and will typically be connected to the keyboard and display from which the virtual machine was launched. If the virtual machine is started automatically, for example by a background job scheduler, then it will typically not have a console.

If this virtual machine has a console then it is represented by a unique instance of this class which can be obtained by invoking the System.console() method. If no console device is available then an invocation of that method will return null.

String readLine()
Reads a single line of text from the console.

char[] readPassword()
Reads a password or passphrase from the console with echoing disabled

下面是一个关于Console的示例代码:

Java代码
Console console = System.console(); // 获得Console实例对象
if (console != null) { // 判断是否有控制台的使用权
String user = new String(console.readLine("Enter username:")); // 读取整行字符
String pwd = new String(console.readPassword("Enter passowrd:")); // 读取密码,输入时不显示
console.printf("Username is: " + user + "\n"); // 显示用户名
console.printf("Password is: " + pwd + "\n"); // 显示密码
} else {
System.out.println("Console is unavailable."); // 提示无控制台使用权限
}

更详细的说明请参考:·http://download.oracle.com/javase/6/docs/api/java/io/Console.html

In Bash
从标准输入读取
使用read命令读取字符串到变量中。但是,如果有反斜杠,将起到转义的