繁体   English   中英

Java-编写一种方法来计数文本文件中的行数而不会引发异常

[英]Java - Writing a Method to Count Lines In a Text File Without Throwing Exceptions

以下是Java文件中的行数解决方案,可快速计算文本文件中的行数。

但是,我试图编写一种方法,该方法将执行相同的任务而不会引发“ IOException”。

在原始解决方案下,我尝试使用嵌套的try-catch块<-(通常完成/皱眉/或容易避免吗?)执行此操作,无论给定文件中有多少行,返回0失败)。

只是要清楚一点,我不是在寻求有关如何更好地使用包含异常的原始方法的建议,因此,我在其中使用它的上下文与该问题无关。

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

马丁计数器的原始行计数器:

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;
        }
    }

对于'\\ 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
            }
    }
}

分享我的尝试。

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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM