【问题标题】:Java/Eclipse - Resolving IOException when using BufferedReader outside of main methodJava/Eclipse - 在主方法之外使用 BufferedReader 时解决 IOException
【发布时间】:2014-01-28 20:10:02
【问题描述】:

我目前正在创建一个程序,该程序从用户那里获取后缀(“ing”、“er”等),然后将该后缀附加到输入文件中的单词列表中。问题是我正在使用缓冲读取器来获取用户想要使用的后缀,但我不断收到错误(线程“main”java.io.IOException 中的异常:流已关闭) 当我编译时。您会在下面注意到,我不会在任何地方关闭缓冲阅读器,因为那是当我收到错误时(我应该在哪里关闭它?)我只是想获取用户的输入并将其设置为要在其中使用的字符串变量班上。有什么建议吗??

public static String GetSuffix (String psSingular1) throws IOException {

    //Use BufferedReader to get suffix
    BufferedReader brGetSuffix = new BufferedReader(new InputStreamReader(System.in));

    //Prompt the user for a suffix
    System.out.println("Please insert a suffix that you would like add to the words (ex. er, ing, ance,etc.)");

    //Set a string to the user entered suffix
    String sUserSuffix = brGetSuffix.readLine();

    //Initialize sSuffix
    String sSuffix = "";

    //Each string represents the last characters of the word from the input file
    String sLast1 = psSingular1.toLowerCase().substring(psSingular1.length()-1, psSingular1.length());


    String s2ndLast = psSingular1.toLowerCase().substring(psSingular1.length()-2, psSingular1.length()-1);

    //This string represents the last letter of the word
    String sLastLetter = psSingular1.toLowerCase().substring(psSingular1.length()-1, psSingular1.length());

    //This string represents a double vowel. It will be used in a word like "keep"
    String sDoubleVowel = psSingular1.toLowerCase().substring(psSingular1.length()-3, psSingular1.length()-1);

    //This String will represent the first character of the users suffix which will be needed to determine how the suffix is added to the word
    String s1stSuffix = sUserSuffix.toLowerCase().substring(0, 0);


    //If the word ends in 2 vowels and a consonant the suffix is added
    if (bIsVowel(sDoubleVowel) && !bIsVowel(sLast1)){

        sSuffix = psSingular1 + sUserSuffix;
    }


        //Checks if psSingular1 ends in a consonant and y, changes the y to i and adds a suffix.
        else if (sLast1.equals("y") && !bIsVowel(s2ndLast)){

        //Drop the "y" add "ies"
        sSuffix = psSingular1.substring(0, psSingular1.length()-1) + "i" + sUserSuffix;
        }

        //Checks if psSingular1 ends in a vowel and y
        else if (sLast1.equals("y") && bIsVowel(s2ndLast)){

            sSuffix = psSingular1 + sUserSuffix;
        }

        //Checks if psSingular1 ends in e and if the suffix starts with a vowel
        else if (sLast1.equals("e") && bIsVowel(s1stSuffix)){

            sSuffix = psSingular1.substring(0, psSingular1.length()) + sUserSuffix;
        }

        //Checks if psSingular1 ends in e and if the suffix starts with a consonant
        else if (sLast1.equals("e") && !bIsVowel(s1stSuffix)){

            sSuffix = psSingular1 + sUserSuffix;
        }

        //Checks if the word ends in two consonants
        else if (!bIsVowel(s2ndLast) && !bIsVowel(sLast1)){

            sSuffix = psSingular1 + sUserSuffix;

        }

        //If the the word ends in a vowel and a consonant the last letter is doubled and the suffix is added
        else if (bIsVowel(s1stSuffix) && bIsVowel(s2ndLast) && !bIsVowel(sLast1)){


            sSuffix = psSingular1 + sLastLetter + sUserSuffix;
        }

        //Add the suffix
        else {

            sSuffix = psSingular1 + sUserSuffix;

        }


    return sSuffix;

}

【问题讨论】:

  • 您可以尝试使用:BufferedReader brGetSuffix = new BufferedReader(new InputStreamReader(new CloseShieldInputStream(System.in))); 并告诉我们它是否仍然不起作用?
  • @ccjmne 复制并粘贴您所拥有的内容,但在 CloseShieldInputStream 下出现错误。它是可能需要导入的类的一部分吗?
  • 糟糕...抱歉,您是对的...来自org.apache.commons.io.input。实际上,也许你可以使用Scanner scanner = new Scanner(System.in); 然后String sUserSuffix = scanner.nextLine();。如果您只是从用户输入中获取一些字符,则可能不需要使用 BufferedReader。我知道它没有回答你的问题,但也许至少你可以继续:)
  • @ccjmne 我决定使用扫描仪并且我能够编译,但我现在收到错误:(线程“main”java.util.NoSuchElementException 中的异常:未找到行)。我确实输入了一些东西,所以我不明白为什么“找不到行”。我也关闭了扫描仪,有什么想法吗?
  • 怎么样:if scanner.hasNextLine() { sUserSuffix = scanner.nextLine() } 它必须在某一时刻认真工作......

标签: java error-handling bufferedreader ioexception


【解决方案1】:

似乎我们遗漏了一些代码(psSingular1 是从哪里来的?),但也许可以尝试在某处关闭缓冲阅读器,看看是否有帮助。完成向阅读器添加输入后,您可以将其关闭。它甚至可以在 return 语句之前。

【讨论】:

  • psSingular1 是他函数的一个参数。我认为他给了我们相关的代码量(但由于某些事情显然是错误的,因此流的实际关闭可能是在其他地方完成的)。
  • 谢谢你。我真是太傻了——不知怎的,我忽略了它。我不认为流关闭是在其他地方完成的,因为流是本地对象,并且整个方法似乎已发布。
  • PsSingular 来自类开头的字符串参数。我认为它与我提到的错误无关。我曾尝试在几个不同的区域关闭缓冲阅读器,但正如我所提到的,我一直收到同样的错误。
  • 您也没有尝试关闭底层 Stream 吗? System.in.close() 任何地方?
猜你喜欢
  • 2013-09-28
  • 1970-01-01
  • 2011-10-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多