📜  文件java代码示例中的行数

📅  最后修改于: 2022-03-11 14:52:46.262000             🧑  作者: Mango

代码示例2
// Get lines in a file
public int linesInFile(String path) {
    try {
        return (int) Files.lines(Paths.get(path)).count(); // Get lines and convert to integer
    } catch(IOException e) {
        e.printStackTrace(); // Print error if file does not exist.
    }
    return -1; // Return -1, if file does not exist.
}