【问题标题】:Java - Writing a Method to Count Lines In a Text File Without Throwing ExceptionsJava - 编写一个方法来计算文本文件中的行数而不抛出异常
【发布时间】:2014-10-27 03:14:27
【问题描述】:

以下是来自Number of lines in a file in Java 的解决方案 快速计算文本文件中的行数。

但是,我正在尝试编写一个方法来执行相同的任务而不会抛出“IOException”。

在原始解决方案下,我尝试使用嵌套的 try-catch 块

为了清楚起见,我并不是在寻找有关如何更好地使用包含异常的原始方法的建议,因此,我使用它的上下文与这个问题无关。

有人可以帮我写一个计算文本文件行数并且不抛出任何异常的方法吗? (换句话说,使用 try-catch 处理潜在的错误。)

ma​​rtinus 的原行计数器:

public static int countLines(String filename) throws IOException {
    InputStream is = new BufferedInputStream(new FileInputStream(filename));
    try {
        byte[] c = new byte[1024];
        int count = 0;
        int readChars = 0;
        boolean empty = true;
        while ((readChars = is.read(c)) != -1) {
            empty = false;
            for (int i = 0; i < readChars; ++i) {
                if (c[i] == '\n') {
                    ++count;
                }
            }
        }
        return (count == 0 && !empty) ? 1 : count;
    } finally {
        is.close();
    }
}

我的尝试:

public int countLines(String fileName ) {
   InputStream input = null;
        try{
        try{
            input = new BufferedInputStream(new FileInputStream(fileName));
            byte[] count = new byte[1024];
            int lines = 0;
            int forChar;
            boolean empty = true;
            while((forChar = input.read(count)) != -1){
                empty = false;
                for(int x = 0; x < forChar; x++){
                    if(count[x] == '\n'){
                        lines++;
                    }
                }
            }
            return (!empty && lines == 0) ?  1 : lines + 1;
        }
        finally{
            if(input != null)
            input.close();
        }
        }
        catch(IOException f){
            int lines = 0;
            return lines;
        }
    }

【问题讨论】:

    标签: java netbeans file-io exception-handling line-count


    【解决方案1】:

    对于 '\n' 使用 char 而不是 byte 更为稳健,并在出现任何错误时返回 -1,例如,如果文件名不存在:

        public static int countLines(String filename) {
        BufferedReader br = null;
        try {
            br = new BufferedReader(new InputStreamReader(new FileInputStream(filename)));
            char[] c = new char[1024];
            int count = 0;
            int readChars = 0;
            boolean emptyLine = true;
            while ((readChars = br.read(c)) != -1) {
                for (int i = 0; i < readChars; ++i) {
                    emptyLine = false;
                    if (c[i] == '\n') {
                        ++count;
                        emptyLine = true;
                    }
                }
            }
            return count + (!emptyLine ? 1 : 0);
        } catch (IOException ex) {
            return -1;
        } finally {
            if (br != null)
                try {
                    br.close();
                } catch (IOException e) {
                    // Ignore intentionally
                }
        }
    }
    

    【讨论】:

      【解决方案2】:

      分享我的尝试。

      public static int countLines(String filename) {
          InputStream is = new BufferedInputStream(new FileInputStream(filename));
          int numLines = 0;
          try {
              byte[] c = new byte[1024];
              int count = 0;
              int readChars = 0;
              boolean empty = true;
              while ((readChars = is.read(c)) != -1) {
                  empty = false;
                  for (int i = 0; i < readChars; ++i) {
                      if (c[i] == '\n') {
                          ++count;
                      }
                  }
              }
              numLines = (count == 0 && !empty) ? 1 : count;
          } catch (IOException ex) {
              numLines = 0;
          } catch (FileNotFoundException ex) {
              System.out.println("File not found.");
              numLines = 0;
          } finally {
              is.close();
          }
          return numLines;
      }
      

      【讨论】:

      • 啊哈!不知道你可以在这样的捕获之后添加 finally ..(我是新人)。
      • 当它走到catch时,总会执行finally语句.. :)
      • 新 FileInputStream(filename) 上未处理的异常类型 FileNotFoundException。
      • 很酷,从来没有见过这样的多合一条件语句语法:) 谢谢!
      猜你喜欢
      • 2012-07-30
      • 2020-02-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多