Example usage for java.io LineNumberReader getLineNumber

List of usage examples for java.io LineNumberReader getLineNumber

Introduction

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

Prototype

public int getLineNumber() 

Source Link

Document

Get the current line number.

Usage

From source file:SumLine.java

static void sumLines(String filename) throws IOException {
    LineNumberReader lnr = new LineNumberReader(new FileReader(filename));
    lnr.setLineNumber(1);//from w  ww.  j av a2s  . co  m
    StreamTokenizer stok = new StreamTokenizer(lnr);
    stok.parseNumbers();
    stok.eolIsSignificant(true);
    stok.nextToken();
    while (stok.ttype != StreamTokenizer.TT_EOF) {
        int lineno = lnr.getLineNumber();
        double sum = 0;
        while (stok.ttype != StreamTokenizer.TT_EOL) {
            if (stok.ttype == StreamTokenizer.TT_NUMBER)
                sum += stok.nval;
            stok.nextToken();
        }
        System.out.println("Sum of line " + lineno + " is " + sum);
        stok.nextToken();
    }
}

From source file:com.shmsoft.dmass.services.Util.java

public static int countLines(String filename) throws IOException {
    LineNumberReader reader = new LineNumberReader(new FileReader(filename));
    int cnt = 0;/*w  w w .  ja va 2 s  .  c  om*/
    String lineRead = "";
    while ((lineRead = reader.readLine()) != null) {
    }
    cnt = reader.getLineNumber();
    reader.close();
    return cnt;
}

From source file:com.sunchenbin.store.feilong.core.io.IOReaderUtil.java

/**
 *  {@link LineNumberReaderResolver}? {@link Reader}.
 *
 * @param reader//  w  ww  . j a v  a2 s  . c o  m
 *            the reader
 * @param lineNumberReaderResolver
 *            the line number reader resolver
 * @since 1.4.1
 */
public static void resolverFile(Reader reader, LineNumberReaderResolver lineNumberReaderResolver) {
    LineNumberReader lineNumberReader = new LineNumberReader(reader);
    try {
        String line = null;
        while ((line = lineNumberReader.readLine()) != null) {
            int lineNumber = lineNumberReader.getLineNumber();
            lineNumberReaderResolver.excute(lineNumber, line);
        }
    } catch (IOException e) {
        LOGGER.error("", e);
        throw new UncheckedIOException(e);
    } finally {
        IOUtils.closeQuietly(lineNumberReader);
        IOUtils.closeQuietly(reader);
    }
}

From source file:Diff.java

public static void readersAsText(Reader r1, String name1, Reader r2, String name2, List diffs)
        throws IOException {
    LineNumberReader reader1 = new LineNumberReader(r1);
    LineNumberReader reader2 = new LineNumberReader(r2);
    String line1 = reader1.readLine();
    String line2 = reader2.readLine();
    while (line1 != null && line2 != null) {
        if (!line1.equals(line2)) {
            diffs.add("File \"" + name1 + "\" and file \"" + name2 + "\" differ at line "
                    + reader1.getLineNumber() + ":" + "\n" + line1 + "\n" + line2);
            break;
        }//  ww  w .j a  v  a  2s .  c  o  m
        line1 = reader1.readLine();
        line2 = reader2.readLine();
    }
    if (line1 == null && line2 != null)
        diffs.add("File \"" + name2 + "\" has extra lines at line " + reader2.getLineNumber() + ":\n" + line2);
    if (line1 != null && line2 == null)
        diffs.add("File \"" + name1 + "\" has extra lines at line " + reader1.getLineNumber() + ":\n" + line1);
}

From source file:org.kalypso.wspwin.core.WspWinProfProj.java

/** Reads the first line of a profproj.txt or .str file. Returns 2 ints: profile count + second count. */
public static int[] readStrHeader(final LineNumberReader reader) throws IOException, ParseException {
    final String firstLine = reader.readLine();
    if (firstLine == null || firstLine.length() == 0)
        throw new ParseException(Messages.getString("org.kalypso.wspwin.core.WspCfg.8"), //$NON-NLS-1$
                reader.getLineNumber());

    // ignore the values, we read the count from the linecount
    // just parse the type
    final StringTokenizer firstLineTokenizer = new StringTokenizer(firstLine);
    if (firstLineTokenizer.countTokens() < 2)
        throw new ParseException(Messages.getString("org.kalypso.wspwin.core.WspCfg.9"), //$NON-NLS-1$
                reader.getLineNumber());

    final int[] counts = new int[2];
    counts[0] = Integer.parseInt(firstLineTokenizer.nextToken());
    counts[1] = Integer.parseInt(firstLineTokenizer.nextToken());

    // if it is a .str file, we ignore the following name and waterName

    return counts;
}

From source file:Main.java

public static int getLineCount(String path) {
    LineNumberReader lnr = null;
    try {/*  w  w w.j  a  v  a 2s .  c o m*/
        lnr = new LineNumberReader(new FileReader(new File(path)));
        lnr.skip(Long.MAX_VALUE);
    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    if (lnr == null)
        return 0;
    return lnr.getLineNumber();
}

From source file:gpl.pierrick.brihaye.aramorph.InMemoryDictionaryHandler.java

/** Loads a compatibility table into a <CODE>Set</CODE>.
 * @param set The set/*w  w w . j av a 2s.  co m*/
 * @param name A human-readable name
 * @param is The stream
 * @throws RuntimeException If a problem occurs when reading the compatibility table
 */
private static void loadCompatibilityTable(Set set, String name, InputStream is) throws RuntimeException {
    System.out.print("Loading compatibility table : " + name + " ");
    try {
        LineNumberReader IN = new LineNumberReader(new InputStreamReader(is, "ISO8859_1"));
        String line = null;
        while ((line = IN.readLine()) != null) {
            if ((IN.getLineNumber() % 1000) == 1)
                System.out.print(".");
            if (!line.startsWith(";")) { //Ignore comments
                line = line.trim();
                line = line.replaceAll("\\s+", " ");
                set.add(line);
            }
        }
        IN.close();
        System.out.println();
        System.out.println(set.size() + " entries");
    } catch (IOException e) {
        throw new RuntimeException("Can not open : " + name);
    }
}

From source file:org.kalypso.wspwin.core.ProfileBean.java

public static ProfileBean[] readProfiles(final LineNumberReader reader, final int profilCount)
        throws IOException, ParseException {
    final List<ProfileBean> beans = new ArrayList<>(20);
    for (int i = 0; i < profilCount; i++) {
        if (!reader.ready())
            throw new ParseException(
                    Messages.getString("org.kalypso.wspwin.core.ProfileBean.0") + reader.getLineNumber(), //$NON-NLS-1$
                    reader.getLineNumber());

        final String line = reader.readLine();
        if (line == null || line.trim().length() == 0)
            throw new ParseException(
                    Messages.getString("org.kalypso.wspwin.core.ProfileBean.1") + reader.getLineNumber(), //$NON-NLS-1$
                    reader.getLineNumber());

        final StringTokenizer tokenizer = new StringTokenizer(line);
        if (tokenizer.countTokens() != 6)
            throw new ParseException(
                    Messages.getString("org.kalypso.wspwin.core.ProfileBean.2") + reader.getLineNumber(), //$NON-NLS-1$
                    reader.getLineNumber());

        try {/*  w w w  .j a va  2 s . c  o  m*/
            final String waterName = tokenizer.nextToken();
            final BigDecimal station = new BigDecimal(tokenizer.nextToken());
            final String mfb = tokenizer.nextToken(); // Mehrfeldbrckenkennung
            final int vzk = parseVZK(tokenizer.nextToken()); // Verzweigungskennung
            final String zustandName = tokenizer.nextToken();
            final String fileName = tokenizer.nextToken();

            final ProfileBean bean = new ProfileBean(waterName, zustandName, station, fileName, mfb, vzk);
            beans.add(bean);
        } catch (final NumberFormatException e) {
            throw new ParseException(
                    Messages.getString("org.kalypso.wspwin.core.ProfileBean.6") + reader.getLineNumber(), //$NON-NLS-1$
                    reader.getLineNumber());
        }
    }

    return beans.toArray(new ProfileBean[beans.size()]);
}

From source file:com.l2jfree.gameserver.util.GPLLicenseChecker.java

private static List<String> read(File f) throws IOException {
    ArrayList<String> list = new ArrayList<String>();

    LineNumberReader lnr = null;
    try {/*from ww  w  . j ava 2s .c  o m*/
        lnr = new LineNumberReader(new FileReader(f));

        for (String line; (line = lnr.readLine()) != null;)
            list.add(line);
    } finally {
        IOUtils.closeQuietly(lnr);
    }

    int i = 0;
    for (; i < LICENSE.length; i++) {
        if (!list.get(i).equals(LICENSE[i])) {
            MODIFIED.add(f.getPath() + ":" + i);
            return list;
        }
    }

    if (!startsWithPackageName(list.get(i))) {
        MODIFIED.add(f.getPath() + ":" + lnr.getLineNumber());
        return list;
    }

    return null;
}

From source file:com.termmed.utils.FileHelper.java

/**
 * Count lines.//from   ww  w  .j av a2s  .com
 *
 * @param file the file
 * @param firstLineHeader the first line header
 * @return the int
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static int countLines(File file, boolean firstLineHeader) throws IOException {

    FileInputStream fis = new FileInputStream(file);
    InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
    LineNumberReader reader = new LineNumberReader(isr);
    int cnt = 0;
    String lineRead = "";
    while ((lineRead = reader.readLine()) != null) {
    }

    cnt = reader.getLineNumber();
    reader.close();
    isr.close();
    fis.close();
    if (firstLineHeader) {
        return cnt - 1;
    } else {
        return cnt;
    }
}