Example usage for java.io LineNumberReader skip

List of usage examples for java.io LineNumberReader skip

Introduction

In this page you can find the example usage for java.io LineNumberReader skip.

Prototype

public long skip(long n) throws IOException 

Source Link

Document

Skip characters.

Usage

From source file:org.yccheok.jstock.gui.Utils.java

/**
 * Returns number of lines in a file.//  w w  w.j  a va 2 s  .  c om
 * 
 * @param file The file
 * @return number of lines in a file
 */
public static int numOfLines(File file, boolean skipMetadata) {
    int line = 0;
    int metaLineNumber = 0;

    LineNumberReader lnr = null;
    try {
        lnr = new LineNumberReader(new FileReader(file));

        if (skipMetadata) {
            String nextLine = lnr.readLine();
            // Metadata handling.
            while (nextLine != null) {
                String[] tokens = nextLine.split("=", 2);
                if (tokens.length == 2) {
                    String key = tokens[0].trim();
                    if (key.length() > 0) {
                        // Is OK for value to be empty.
                        metaLineNumber++;
                        nextLine = lnr.readLine();
                    } else {
                        break;
                    }
                } else {
                    break;
                }
            }
        }

        lnr.skip(Long.MAX_VALUE);
        line = lnr.getLineNumber();
    } catch (IOException ex) {
        log.error(null, ex);
    } finally {
        org.yccheok.jstock.file.Utils.close(lnr);
    }

    return line - metaLineNumber;
}