簡體   English   中英

自包含Java控制台輸入法?

[英]Self-contained Java console input method?

我正在嘗試編寫一個獨立的(即不需要外部打開/關閉資源)方法來從控制台獲取輸入,但是遇到了多次使用它的問題。 我使用BufferedReader和Scanner測試了它:

public class Test {

    public static void main(String[] args) {
//      String str1 = getConsoleInputSc("Enter the 1st string: ");
        String str1 = getConsoleInputBR("Enter the 1st string: ");
        System.out.println(str1);
//      String str2 = getConsoleInputSc("Enter the 2nd string: ");
        String str2 = getConsoleInputBR("Enter the 2nd string: ");
        System.out.println(str2);
    }

    public static String getConsoleInputBR(String prompt) {
        String input = "";
        try (BufferedReader bufferRead = new BufferedReader(
                new InputStreamReader(System.in))) {
            System.out.print(prompt);
            input = bufferRead.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return input;
    }

    public static String getConsoleInputSc(String prompt) {
        String input = "";
        try (Scanner scanIn = new Scanner(System.in)) {
            System.out.print(prompt);
            input = scanIn.nextLine();
        } catch (RuntimeException e) {
            e.printStackTrace();
        }
        return input;
    }    
}

我正在使用try-with-resources語句來處理關閉資源。

這是使用BufferedReader版本的示例運行:

Enter the 1st string: efsd
efsd
Enter the 2nd string: java.io.IOException: Stream closed
        at java.io.BufferedInputStream.getBufIfOpen(Unknown Source)
        at java.io.BufferedInputStream.read(Unknown Source)
        at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
        at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
        at sun.nio.cs.StreamDecoder.read(Unknown Source)
        at java.io.InputStreamReader.read(Unknown Source)
        at java.io.BufferedReader.fill(Unknown Source)
        at java.io.BufferedReader.readLine(Unknown Source)
        at java.io.BufferedReader.readLine(Unknown Source)
        at Test.getConsoleInputBR(Test.java:25)
        at Test.main(Test.java:16)

以下是使用掃描儀版本的示例運行:

Enter the 1st string: sdfds
sdfds
Enter the 2nd string: java.util.NoSuchElementException: No line found
        at java.util.Scanner.nextLine(Unknown Source)
        at Test.getConsoleInputSc(Test.java:3)
        at Test.main(Test.java:14)

我根據我在查找“java.util.NoSuchElementException:No line found”異常(帶掃描程序)時發現的帖子嘗試了一些建議。 使用“while(scanIn.hasNextLine())...”我只是在第一次調用中得到一個連續循環。 使用“if(scanIn.hasNextLine())”它在第二次調用中從不使它成為“input = scanIn.nextLine()”,因為if子句返回false。

我覺得這些問題與Java GC的不確定性有關,導致資源無法正確清理/釋放,但這只是猜測。

有任何想法嗎?

謝謝

這是關於try-with-resources的教程。

當您聲明以下內容時

try (BufferedReader bufferRead = new BufferedReader(
            new InputStreamReader(System.in))) {

當代碼退出try塊, close()將被稱為上bufferRead ,這將調用close()被包裝上InputStreamReader對象,這將調用close()通過引用的對象上System.in ,基本上關閉你的標准輸入。

在您確定不需要它之前,請不要關閉標准輸入,基本上從不。

你必須重新考慮你的設計。

而不是像你一樣使用try-with-resources,只需將代碼放在一個簡單的try-catch塊中,如下所示:

   public static String getConsoleInput(String prompt)
   {
      String input = "";
      BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
      System.out.print(prompt);
      try {
         input = br.readLine();
      }
      catch (IOException e)
      {
         e.printStackTrace();
      }
      return input;
   }

當main()中的代碼調用時,此方法工作正常。 通過不使用try with resources,資源不會自動關閉。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM