Number of lines in a file in Java

前端 未结 19 2340
抹茶落季
抹茶落季 2020-11-22 05:31

I use huge data files, sometimes I only need to know the number of lines in these files, usually I open them up and read them line by line until I reach the end of the file<

相关标签:
19条回答
  • 2020-11-22 05:46

    I know this is an old question, but the accepted solution didn't quite match what I needed it to do. So, I refined it to accept various line terminators (rather than just line feed) and to use a specified character encoding (rather than ISO-8859-n). All in one method (refactor as appropriate):

    public static long getLinesCount(String fileName, String encodingName) throws IOException {
        long linesCount = 0;
        File file = new File(fileName);
        FileInputStream fileIn = new FileInputStream(file);
        try {
            Charset encoding = Charset.forName(encodingName);
            Reader fileReader = new InputStreamReader(fileIn, encoding);
            int bufferSize = 4096;
            Reader reader = new BufferedReader(fileReader, bufferSize);
            char[] buffer = new char[bufferSize];
            int prevChar = -1;
            int readCount = reader.read(buffer);
            while (readCount != -1) {
                for (int i = 0; i < readCount; i++) {
                    int nextChar = buffer[i];
                    switch (nextChar) {
                        case '\r': {
                            // The current line is terminated by a carriage return or by a carriage return immediately followed by a line feed.
                            linesCount++;
                            break;
                        }
                        case '\n': {
                            if (prevChar == '\r') {
                                // The current line is terminated by a carriage return immediately followed by a line feed.
                                // The line has already been counted.
                            } else {
                                // The current line is terminated by a line feed.
                                linesCount++;
                            }
                            break;
                        }
                    }
                    prevChar = nextChar;
                }
                readCount = reader.read(buffer);
            }
            if (prevCh != -1) {
                switch (prevCh) {
                    case '\r':
                    case '\n': {
                        // The last line is terminated by a line terminator.
                        // The last line has already been counted.
                        break;
                    }
                    default: {
                        // The last line is terminated by end-of-file.
                        linesCount++;
                    }
                }
            }
        } finally {
            fileIn.close();
        }
        return linesCount;
    }
    

    This solution is comparable in speed to the accepted solution, about 4% slower in my tests (though timing tests in Java are notoriously unreliable).

    0 讨论(0)
  • 2020-11-22 05:46

    It seems that there are a few different approaches you can take with LineNumberReader.

    I did this:

    int lines = 0;
    
    FileReader input = new FileReader(fileLocation);
    LineNumberReader count = new LineNumberReader(input);
    
    String line = count.readLine();
    
    if(count.ready())
    {
        while(line != null) {
            lines = count.getLineNumber();
            line = count.readLine();
        }
        
        lines+=1;
    }
        
    count.close();
    
    System.out.println(lines);
    

    Even more simply, you can use the Java BufferedReader lines() Method to return a stream of the elements, and then use the Stream count() method to count all of the elements. Then simply add one to the output to get the number of rows in the text file.

    As example:

    FileReader input = new FileReader(fileLocation);
    LineNumberReader count = new LineNumberReader(input);
    
    int lines = (int)count.lines().count() + 1;
        
    count.close();
    
    System.out.println(lines);
    
    0 讨论(0)
  • 2020-11-22 05:49

    Scanner with regex:

    public int getLineCount() {
        Scanner fileScanner = null;
        int lineCount = 0;
        Pattern lineEndPattern = Pattern.compile("(?m)$");  
        try {
            fileScanner = new Scanner(new File(filename)).useDelimiter(lineEndPattern);
            while (fileScanner.hasNext()) {
                fileScanner.next();
                ++lineCount;
            }   
        }catch(FileNotFoundException e) {
            e.printStackTrace();
            return lineCount;
        }
        fileScanner.close();
        return lineCount;
    }
    

    Haven't clocked it.

    0 讨论(0)
  • 2020-11-22 05:50

    The answer with the method count() above gave me line miscounts if a file didn't have a newline at the end of the file - it failed to count the last line in the file.

    This method works better for me:

    public int countLines(String filename) throws IOException {
        LineNumberReader reader  = new LineNumberReader(new FileReader(filename));
    int cnt = 0;
    String lineRead = "";
    while ((lineRead = reader.readLine()) != null) {}
    
    cnt = reader.getLineNumber(); 
    reader.close();
    return cnt;
    }
    
    0 讨论(0)
  • 2020-11-22 05:53

    A straight-forward way using Scanner

    static void lineCounter (String path) throws IOException {
    
            int lineCount = 0, commentsCount = 0;
    
            Scanner input = new Scanner(new File(path));
            while (input.hasNextLine()) {
                String data = input.nextLine();
    
                if (data.startsWith("//")) commentsCount++;
    
                lineCount++;
            }
    
            System.out.println("Line Count: " + lineCount + "\t Comments Count: " + commentsCount);
        }
    
    0 讨论(0)
  • 2020-11-22 05:56

    I have implemented another solution to the problem, I found it more efficient in counting rows:

    try
    (
       FileReader       input = new FileReader("input.txt");
       LineNumberReader count = new LineNumberReader(input);
    )
    {
       while (count.skip(Long.MAX_VALUE) > 0)
       {
          // Loop just in case the file is > Long.MAX_VALUE or skip() decides to not read the entire file
       }
    
       result = count.getLineNumber() + 1;                                    // +1 because line index starts at 0
    }
    
    0 讨论(0)
提交回复
热议问题