Total Pageviews

2019/05/11

[Java] How to get the count of line in file?

Here has a simple example to demonstrate how to get the count of lines in file:

package com.example.demo;

import lombok.experimental.UtilityClass;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@UtilityClass
public class FileUtils {

    public static Long getLines(String filePath) throws IOException {
        Path path = Paths.get(filePath);
        Long count = Files.lines(path).count();
        return count;
    }

}


No comments: