zoukankan      html  css  js  c++  java
  • Java 字符终端上获取输入三种方式

    http://blog.csdn.net/hongweigg/article/details/14448731

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

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

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

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

    package com.srie.chapter01;
    
    import java.io.BufferedReader;
    import java.io.Console;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Scanner;
    
    public class TestCalc {
    
        public static void main(String[] args) throws Exception {
            
            readBySystem();
            readByScanner();
            readByConsole();
        }
    
        private static void readByConsole() {
            System.out.println("read by console");
            Console console = System.console();
            if(console == null){
                System.out.println("console is null!");
            }else{
                String readLine = console.readLine();
                System.out.println(readLine);
            }
        }
    
        private static void readBySystem() throws IOException {
            System.out.println("read by system.in");
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
            String readLine = bufferedReader.readLine();
            System.out.println(readLine);
        }
    
        private static void readByScanner() {
            System.out.println("read by scanner:");
            Scanner scanner = new Scanner(System.in);
            String nextLine = scanner.nextLine();
            System.out.println(nextLine);
        }
    
    }
  • 相关阅读:
    Python深拷贝和浅拷贝解析
    python中count函数的用法
    Jenkins + gitlab + maven 自动打包部署项目
    nio和bio得区别
    nginx负载均衡的5种策略
    接口测试常见bug
    接口自动化面试4
    pass 语句
    if 语句
    while循环
  • 原文地址:https://www.cnblogs.com/stono/p/4332641.html
Copyright © 2011-2022 走看看