0opslab / opslabJutil

Java utils
840 stars 483 forks source link

function countLines in [com.opslab.util.FileUtil] at line 71 may not calc the right lines #7

Open riclava opened 8 years ago

riclava commented 8 years ago

these code works

InputStream input = new FileInputStream(file);
BufferedReader b = new BufferedReader(new InputStreamReader(input));
String value = b.readLine();
if (value != null)
    while (value != null) {
        count++;
        value = b.readLine();
    }
b.close();
input.close();
0opslab commented 8 years ago

thanks! it's not the best way! maybe!

public final static int countLines(File file) {
        try(LineNumberReader rf = new LineNumberReader(new FileReader(file))){
            long fileLength = file.length();
            rf.skip(fileLength);
            return rf.getLineNumber();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return 0;
    }