Example usage for java.io LineNumberReader readLine

List of usage examples for java.io LineNumberReader readLine

Introduction

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

Prototype

public String readLine() throws IOException 

Source Link

Document

Read a line of text.

Usage

From source file:Main.java

/**
 * Reads the first non-empty line from a file.
 *
 * @param file the file from which the line is to be read. This argument
 *   cannot be {@code null}./*w  ww .j  a v a2  s  .c om*/
 * @return the first non-empty line in the given file or {@code null} if
 *   there is no such line in the specified file
 *
 * @throws IOException thrown if there was an error while reading the
 *   specified file (e.g.: it does not exist)
 */
private static String readFirstLine(Path file) throws IOException {
    try (InputStream input = Files.newInputStream(file)) {
        LineNumberReader reader = new LineNumberReader(new InputStreamReader(input, DEFAULT_CHARSET));
        String line = reader.readLine();
        while (line != null) {
            line = line.trim();
            if (!line.isEmpty()) {
                return line;
            }
            line = reader.readLine();
        }
    }

    return null;
}

From source file:Main.java

public static String getMac() {
    String mac = null;//w ww  . j  a va  2 s .  c  o  m
    String macSerial = null;
    String str = "";
    try {
        Process pp = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address");
        //            Process pp = Runtime.getRuntime().exec("cat /sys/class/net/eth0/address");
        InputStreamReader ir = new InputStreamReader(pp.getInputStream());
        LineNumberReader input = new LineNumberReader(ir);

        for (; null != str;) {
            str = input.readLine();
            if (str != null) {
                macSerial = str.trim();
                break;
            }
        }
    } catch (IOException ex) {
        ex.printStackTrace();
    }

    if (macSerial != null) {
        macSerial = macSerial.replace(":", "");
        macSerial = macSerial.replace("a", "A");
        macSerial = macSerial.replace("b", "B");
        macSerial = macSerial.replace("c", "C");
        macSerial = macSerial.replace("d", "D");
        macSerial = macSerial.replace("e", "E");
        mac = macSerial.replace("f", "F");
    }

    return mac;

}

From source file:com.porvak.bracket.social.jdbc.versioned.SqlDatabaseChange.java

private static String readScript(Resource resource) throws IOException {
    EncodedResource encoded = resource instanceof EncodedResource ? (EncodedResource) resource
            : new EncodedResource(resource);
    LineNumberReader lnr = new LineNumberReader(encoded.getReader());
    String currentStatement = lnr.readLine();
    StringBuilder scriptBuilder = new StringBuilder();
    while (currentStatement != null) {
        if (StringUtils.hasText(currentStatement)
                && (SQL_COMMENT_PREFIX != null && !currentStatement.startsWith(SQL_COMMENT_PREFIX))) {
            if (scriptBuilder.length() > 0) {
                scriptBuilder.append('\n');
            }//w w  w. jav a  2 s . c o  m
            scriptBuilder.append(currentStatement);
        }
        currentStatement = lnr.readLine();
    }
    return scriptBuilder.toString();
}

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;
        }/*from   w w  w  .  jav a 2s .  c  om*/
        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:Main.java

public static String readFileAsStr(File file) throws IOException {
    StringBuffer sb = new StringBuffer();
    if (file == null)
        return "";
    if (!file.exists())
        return "";

    LineNumberReader lnr = null;
    try {/*from   w w  w.  ja  va 2  s .c om*/
        lnr = new LineNumberReader(new FileReader(file));

        String s = lnr.readLine();
        while (s != null) {
            sb.append(s);
            s = lnr.readLine();
        }
    } catch (java.io.IOException ioe) {
        throw ioe;
    } finally {
        if (lnr != null) {
            lnr.close();
        }
    }
    return sb.toString();
}

From source file:Main.java

/**
 * Loads an index (Hashtable) from a file.
 * //from ww w .  ja  v a 2s .  c om
 * @param root_
 *            The file where the index is stored in.
 * @return The indextable.
 * @exception IOException
 *                If an internal error prevents the file from being read.
 */
public static Hashtable loadIndex(File root_, String name) throws IOException {
    File indexfile = new File(root_, name);
    Hashtable index = new Hashtable();
    if (indexfile.exists()) {
        LineNumberReader in = new LineNumberReader(new FileReader(indexfile));
        while (in.ready())
            index.put(in.readLine(), new Long(in.readLine()));
        in.close();
    }
    return index;
}

From source file:org.kawanfw.sql.jdbc.util.CallableUtil.java

public static CallableStatementHolder fromRsFile(File file, ConnectionHttp connectionHttp) throws SQLException {
    if (!file.exists()) {
        String message = Tag.PRODUCT_PRODUCT_FAIL + "Internal File does not exists: " + file;
        throw new SQLException(message, new IOException(message));
    }/*from   w  ww  .ja  v a 2 s. c o m*/

    String line = null;
    LineNumberReader lineNumberReader = null;

    try {
        lineNumberReader = new LineNumberReader(new FileReader(file));
        line = lineNumberReader.readLine();
    } catch (FileNotFoundException e) {
        String message = Tag.PRODUCT_PRODUCT_FAIL + "Internal File does not exists: " + file;
        throw new SQLException(message, new IOException(message));
    } catch (IOException e) {
        String message = Tag.PRODUCT_PRODUCT_FAIL + "I/O Error when reading file: " + file;
        throw new SQLException(message, new IOException(message));
    } finally {
        IOUtils.closeQuietly(lineNumberReader);
    }

    if (line == null) {
        String message = Tag.PRODUCT_PRODUCT_FAIL + "Internal File is empty: " + file;
        throw new SQLException(message, new IOException(message));
    }

    line = JsonLineDecryptor.decrypt(line, connectionHttp);

    return CallableStatementHolderTransportJson.fromJson(line);
}

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: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 w  ww .  j  av  a 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.shmsoft.dmass.services.Util.java

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