国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 編程 > Java > 正文

Java 字符終端上獲取輸入三種的方式分享

2019-11-26 15:53:06
字體:
供稿:網(wǎng)友

在Java 字符終端上獲取輸入有三種方式:

1、java.lang.System.in (目前JDK版本均支持)
2、java.util.Scanner (JDK版本>=1.5)
3、java.io.Console(JDK版本>=1.6),特色:能不回顯密碼字符

參考:
這里記錄Java中從控制臺(tái)讀入信息的幾種方式
(1)JDK 1.4(JDK 1.5和JDK 1.6也都兼容這種方法)

復(fù)制代碼 代碼如下:

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進(jìn)行讀取)

復(fù)制代碼 代碼如下:

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還可以很方便的掃描文件,讀取里面的信息并轉(zhuǎn)換成你要的類型,比如對(duì)“2 2.2 3.3 3.33 4.5 done”這樣的數(shù)據(jù)求和,見如下代碼:

復(fù)制代碼 代碼如下:

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進(jìn)行讀取)
JDK6中提供了java.io.Console類專用來訪問基于字符的控制臺(tái)設(shè)備.
你的程序如果要與Windows下的cmd或者Linux下的Terminal交互,就可以用Console類代勞.(類似System.in和System.out)
但我們不總是能得到可用的Console, 一個(gè)JVM是否有可用的Console依賴于底層平臺(tái)和JVM如何被調(diào)用.
如果JVM是在交互式命令行(比如Windows的cmd)中啟動(dòng)的,并且輸入輸出沒有重定向到另外的地方,那么就可以得到一個(gè)可用的Console實(shí)例。
在使用 IDE 的情況下,是無(wú)法獲取到Console實(shí)例的,原因在于在 IDE 的環(huán)境下,重新定向了標(biāo)準(zhǔn)輸入和輸出流,也是就是將系統(tǒng)控制臺(tái)上的輸入輸出重定向到了 IDE 的控制臺(tái)中

復(fù)制代碼 代碼如下:

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類還有個(gè)特色就是,專門對(duì)密碼(輸入無(wú)回顯)等安全字符,進(jìn)行了處理。專門提供 readPassword()方法,具體應(yīng)用見如下代碼:

復(fù)制代碼 代碼如下:

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); 
             // 使用后應(yīng)立即將數(shù)組清空,以減少其在內(nèi)存中占用的時(shí)間,增強(qiáng)安全性  
                password = null; 
              System.exit(-1); 
            }  
            else { 
              console.printf("Invalid username or password./n"); 
            } 
            } 
          } 


發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 宝兴县| 苍梧县| 正镶白旗| 承德市| 虞城县| 安丘市| 杨浦区| 福贡县| 全椒县| 民勤县| 石家庄市| 渑池县| 仪征市| 康平县| 婺源县| 舟山市| 柳河县| 延长县| 绥德县| 安远县| 黎城县| 张北县| 建瓯市| 庆城县| 蒙城县| 民权县| 云梦县| 轮台县| 神池县| 二手房| 金平| 攀枝花市| 衡水市| 吴桥县| 宜章县| 淮滨县| 汨罗市| 赞皇县| 富裕县| 曲周县| 四会市|