【问题标题】:Can someone help me to get buffered reader to work in Java?有人可以帮助我让缓冲阅读器在 Java 中工作吗?
【发布时间】:2014-04-03 14:25:25
【问题描述】:

在这里,我正在尝试阅读包含单词和相应缩写的文档。例如。

one,1
two,2
easy,ez

每一行都是分开的,单词和缩写之间用逗号隔开。


当我希望缓冲阅读器使用

继续阅读行时
while ((line = br.readLine()) != null)

不会的。它只会读取第一行。这意味着它将显示一个的缩写,即 1。但如果我输入另一个值,比如 2,我会收到此错误消息。

Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 5
    at java.lang.String.charAt(String.java:658)
    at shortener.NewClass.Short(NewClass.java:41)
    at shortener.NewClass.main(NewClass.java:26)
Java Result: 

这是代码

public static String Short(String input) {

    try {
        FileReader fr = new FileReader("abbreviations.txt");
        BufferedReader br = new BufferedReader(fr);

        String line;
        while ((line = br.readLine()) != null) {

            for(int i = 0; i <= line.length(); i++) {
                if(line.charAt(i) == ','){
                    String strOrig = line.substring(0, i);
                    if(strOrig.equals(input)) {
                        String strAbv = line.substring(line.length()-1);
                        return strAbv;
                        }
                    }                
                }
            }   
        br.close();
        } catch (IOException e) {
        out.println("File Not found");
    }
    return null;
}

【问题讨论】:

    标签: java bufferedreader


    【解决方案1】:

    这是不正确的:

    for(int i = 0; i <= line.length(); i++) {
    

    因为它访问的太多了。更正:

    for(int i = 0; i < line.length(); i++) {
    

    请注意,如果找到缩写,则发布的代码不会close() 读者。使用finally 块或try-with-resources 语句确保阅读器关闭。

    其他:

    • String.split() 可用于将行分隔为两个标记,而不是显式编码分隔。
    • Properties 样式文件可用于存储 abbreviation-to-term 映射,而不是逗号分隔文件。 Properties 类提供了加载文件的机制和搜索地图的方法:

         // If the 'abbreviations.txt' file is not updated
         // by the program then arrange for it to be loaded once.
         //
         try (final FileInputStream is =
                  new FileInputStream("abbreviations.txt"))
         {
             final Properties p = new Properties();
             p.load(is);
             return p.getProperty(input);
         }
      

    【讨论】:

    • +1 建议使用.split()。可以举例说明如何更好地使用它吗?
    【解决方案2】:

    您已将for 循环中的条件表达式设置为i 小于或等于line.length()。错了。i 不应该等于行的长度。改变这个

    for(int i = 0; i <= line.length(); i++)
    

    for(int i = 0; i < line.length(); i++)
    

    你不能使i 等于行的长度。索引从0 开始。因此,有效索引将从0length-1

    【讨论】:

      【解决方案3】:

      查看代码的 for 循环部分:

      for(int i = 0; i <= line.length(); i++) {
      

      应该改成这样:

      for(int i = 0; i < line.length(); i++) {
      

      注意&lt;= 是如何更改为&lt; 的:

      for(int i = 0; i <= line.length(); i++) {
      //vs:
      for(int i = 0; i < line.length(); i++) {
      

      对于几乎所有的编码,包括java,计数从 0 开始。所以在通常你会说 5 的地方,在编码中你会说 4。

      假设数组的长度为3,这意味着它包含012。所以,当这样做时:

      for(int i = 0; i <= line.length(); i++) {
      //length of the line is 3, so:
      for(int i = 0; i <= 3; i++) {
      

      现在,它将循环抛出数字 0123。然而,该行仅包含 012。所以,如果我们这样做:

      for(int i = 0; i < line.length(); i++) {
      //length of the line is 3, so:
      for(int i = 0; i < 3; i++) {
      

      使用这个新代码,它将循环抛出 012,与我们的行长度相同,012

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-02-06
        • 1970-01-01
        • 2020-04-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多