Java 字符终端上获取输入 System.in

来源:互联网 发布:win7注册表优化大全 编辑:程序博客网 时间:2024/05/15 07:09

在Java 字符终端上获取输入有三种方式:

1、java.lang.System.in (目前JDK版本均支持)

2、java.util.Scanner (JDK版本>=1.5)

3、java.io.Console(JDK版本>=1.6),特色:能不回显密码字符

(1)BufferedReader

public class TestConsole1 {  
    public static void main(String[] args) {  
        String str = readDataFromConsole("Please input string:);  
        System.out.println("The information from console: + str);  
    }  
  
    /** 
     * Use InputStreamReader and System.in to read data from console 
     *  
     * @param prompt 
     *             
     * @return input string 
     */  
    private static String readDataFromConsole(String prompt) {  
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
        String str = null;  
        try {  
            System.out.print(prompt);  
            str = br.readLine();  
  
        } catch (IOException e) {  
            e.printStackTrace();  
        }  
        return str;  
    }  
}  




 
(2)JDK 1.5(利用Scanner进行读取)
 
public class TestConsole2 {  
    public static void main(String[] args) {  
        String str = readDataFromConsole("Please input string:");  
        System.out.println("The information from console:" + str);  
    }  
  
    /** 
     * Use  java.util.Scanner to read data from console 
     *  
     * @param prompt 
     *  
     * @return input string 
     */  
    private static String readDataFromConsole(String prompt) {  
        Scanner scanner = new Scanner(System.in);  
        System.out.print(prompt);  
        return scanner.nextLine();  
    }  
}  




 
Scanner还可以很方便的扫描文件,读取里面的信息并转换成你要的类型,比如对“2 2.2 3.3 3.33 4.5 done”这样的数据求和,见如下代码:
 
 
 
public class TestConsole4 {  
  
    public static void main(String[] args) throws IOException {  
        FileWriter fw = new FileWriter("num.txt");  
        fw.write("2 2.2 3.3 3.33 4.5 done");  
        fw.close();  
  
        System.out.println("Sum is "+scanFileForSum("num.txt"));  
    }  
  
    public static double scanFileForSum(String fileName) throws IOException {  
        double sum = 0.0;  
        FileReader fr = null;  
        try {  
            fr = new FileReader(fileName);  
            Scanner scanner = new Scanner(fr);  
              
            while (scanner.hasNext()) {  
                if (scanner.hasNextDouble()) {  
                    sum = sum + scanner.nextDouble();  
  
                } else {  
                    String str = scanner.next();  
  
                    if (str.equals("done")) {  
                        break;  
                    } else {  
                        throw new RuntimeException("File Format is wrong!");  
                    }  
  
                }  
            }  
  
        } catch (FileNotFoundException e) {  
            throw new RuntimeException("File " + fileName + " not found!");  
        } finally {  
            if (fr != null)  
                fr.close();  
        }  
        return sum;  
    }  
}  
(3)JDK 1.6(利用java.io.Console进行读取)
JDK6中提供了java.io.Console类专用来访问基于字符的控制台设备. 
你的程序如果要与Windows下的cmd或者Linux下的Terminal交互,就可以用Console类代劳.(类似System.in和System.out) 
但我们不总是能得到可用的Console, 一个JVM是否有可用的Console依赖于底层平台和JVM如何被调用. 
如果JVM是在交互式命令行(比如Windows的cmd)中启动的,并且输入输出没有重定向到另外的地方,那么就可以得到一个可用的Console实例。
在使用 IDE 的情况下,是无法获取到Console实例的,原因在于在 IDE 的环境下,重新定向了标准输入和输出流,也是就是将系统控制台上的输入输出重定向到了 IDE 的控制台中
 
public class TestConsole3 {  
    public static void main(String[] args) {  
        String str = readDataFromConsole("Please input string:");  
        System.out.println("The information from console:" + str);  
    }  
  
    /** 
     * Use  java.io.console to read data from console 
     *  
     * @param prompt 
     *  
     * @return input string 
     */  
    private static String readDataFromConsole(String prompt) {  
        Console console = System.console();  
        if (console == null) {  
            throw new IllegalStateException("Console is not available!");  
        }  
        return console.readLine(prompt);  
    }  
}  


 
Console类还有个特色就是,专门对密码(输入无回显)等安全字符,进行了处理。专门提供 readPassword()方法,具体应用见如下代码:
 
public class TestConsole5 {  
      
     public static void main(String[] args) {  
            Console console = System.console();  
            if (console == null) {  
                throw new IllegalStateException("Console is not available!");  
            }  
              
            while(true){  
            String username = console.readLine("Username: ");  
            char[] password = console.readPassword("Password: ");  
  
            if (username.equals("Chris") && String.valueOf(password).equals("GoHead")) {  
              console.printf("Welcome to Java Application %1$s.\n", username);  
             // 使用后应立即将数组清空,以减少其在内存中占用的时间,增强安全性   
                password = null;  
              System.exit(-1);  
            }   
            else {  
              console.printf("Invalid username or password.\n");  
            }  
            }  
          }  
  

}  




System.in读取标准输入设备数据(从标准输入获取数据,一般是键盘),其数据类型为InputStream。方法:

  int read()   // 返回输入数值的ASCII码,,该值为0到 255范围内的int字节值。若返回值为-1,说明没有读取到任何字节读取工作结束。

  int read(byte[] b)  // 读入多个字节到缓冲区b中,返回值是读入的字节数

复制代码
package InPackage;/** * System.in.read()返回值为输入数值的ASCII码,该值为0到 255范围内的int字节值 * 如果因为已经到达流末尾而没有可用的字节,则返回值 -1。 */public class Intest1 {    public static void main(String args[]) throws java.io.IOException      {       int a=0;       System.out.println("请输入a:");       a=System.in.read();       System.out.println("a="+a);       System.out.println("(char)a="+(char)a);      }    /**     * 假设我们输入a为1     * 输出结果为:     * 请输入a:     * 1     * a=49     * (char)a=1     */    }
复制代码

  有一个有意思的问题是:当我们输入一个字符,System.in.read()会读取几个字符呢?

复制代码
package InPackage;import java.util.Arrays;/** * 当我们输入一个字符,System.in.read()会读取几个字符 * 我们从运行结果可以看出是三个 * 假设我们输入一个字符,那么它会接着读取该字符后面的/r和/n */public class Intest2 {     public static void main(String[] args) throws Exception {           int[] x = new int[6];           Arrays.fill(x, 5);  //Arrays.fill(int[] a,int b)方法用于给数组中的每个元素赋值         for (int i = 0; i < x.length; i++) {               System.in.read();               System.out.println(x[i]);           }       }     /**      * 假设我们输入值分别为1,2      * 输出结果:      * 1      * 5      * 5      * 5      * 2      * 5      * 5      * 5      */}
复制代码

  System.in.read()每次只是读取一个字符,但它多读取的是哪几个字符呢?

复制代码
package InPackage;import java.io.IOException;/** * System.in.read()每次只是读取一个字符 * 按下回车键代表了两个字符\r\n,\r的ASCII码值是10,\n是13。另外,1对应的ASCII是49 */public class Intest3 {    public static void main(String args[]) throws IOException {        for (int j = 0; j < 5; j++) {            System.out.println("请输入:");            char c = 0;            c = (char) System.in.read();            if (c == '1') {                System.out.println("OK!");            } else {                System.out.println((int) c);            }        }    }}
复制代码

  对于上面的程序,我们首先输入的是w1,结果如下图所示:

  可以看出程序还没有执行完,阻塞于最后一个“请输入:”,此时我们再次输入1,程序执行完成,结果如下图所示:

  如何让System..in.read()读入一行数据呢?

复制代码
package InPackage;import java.io.IOException;public class Intest4 {    public static void main(String args[]) {        int b;        try {            System.out.println("请输入:");            while ((b = System.in.read()) != -1) {                System.out.print((char) b);            }        } catch (IOException e) {            System.out.println(e.toString());        }    }    /**     * 输出结果:     * 请输入:     * test     * test     */}
复制代码
复制代码
package InPackage;import java.io.BufferedReader;import java.io.DataInputStream;import java.io.InputStreamReader;/** * 通常情况下,你会用readLine( )一行一行地读取输入, * 因此要把System.in包装成BufferedReader。但在这之前还得先用InputSteamReader把System.in转换成Reader。 * BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); * in.readLine()返回值为String类型 * */public class Intest5 {    public static void main(String args[]) throws java.io.IOException {        System.out.println("请输入整数:");        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));        //或者这么写也可以:DataInputStream reader = new DataInputStream(System.in);        int a = Integer.parseInt(reader.readLine()); // 这样得到的是String类型的,需要转换为需要的类型        System.out.println("a=" + a);        int sum = 0;        for (int i = 0; i <= a; i++)            sum += i;        System.out.println(sum);    }    /**     * 假设我们输入a为100     * 输出结果为:     * 100     * a=100     * 5050     */}
复制代码

  public int read(byte[] b) throws IOException又是怎么使用的呢?

复制代码
package InPackage;/** * public int read(byte[] b) throws IOException  * 从输入流中读取一定数量的字节,并将其存储在缓冲区数组 b中。 * 返回值为:以整数形式返回实际读取的字节数。  * 如果 b的长度为0,则不读取任何字节并返回 0; 否则,尝试读取至少一个字节。  * 如果因为流位于文件末尾而没有可用的字节,则返回值 -1;否则,至少读取一个字节并将其存储在b中。 *  */public class Intest6 {    public static void main(String args[]) throws Exception {        byte[] barray = new byte[5];        System.out.println("请输入:");        System.in.read(barray);        for (int i = 0; i < barray.length; i++) {            System.out.println((char) barray[i]);        }    }}

                                             
0 0