Example usage for java.io LineNumberReader LineNumberReader

List of usage examples for java.io LineNumberReader LineNumberReader

Introduction

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

Prototype

public LineNumberReader(Reader in) 

Source Link

Document

Create a new line-numbering reader, using the default input-buffer size.

Usage

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;
        }// w  ww .  ja  va  2  s  .  com
        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;/*  w  w w .j a va2  s  .com*/
    try {
        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

@SuppressLint("HardwareIds")
static String getPhoneMacAddress(Context context) {
    String mac = "";

    InputStreamReader inputStreamReader = null;
    LineNumberReader lineNumberReader = null;
    try {//w  ww .jav a 2 s . c  om
        @SuppressLint("WifiManagerPotentialLeak")
        WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = manager.getConnectionInfo();
        mac = info.getMacAddress();

        if (TextUtils.isEmpty(mac)) {
            Process process = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address");
            inputStreamReader = new InputStreamReader(process.getInputStream());
            lineNumberReader = new LineNumberReader(inputStreamReader);
            String line = lineNumberReader.readLine();
            if (line != null) {
                mac = line.trim();
            }
        }

    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (inputStreamReader != null) {
            try {
                inputStreamReader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (lineNumberReader != null) {
            try {
                lineNumberReader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

    if (TextUtils.isEmpty(mac)) {
        mac = "na";
    }

    return mac;
}

From source file:Main.java

static long getLineCountForFile(File file) throws IOException {
    LineNumberReader in = new LineNumberReader(new FileReader(file));
    long numLines = 0;
    while (in.readLine() != null)
        ;//from  w  ww .  j av a  2 s  . c  o  m
    numLines = in.getLineNumber();
    in.close();

    return numLines;

}

From source file:de.micromata.genome.util.text.StandardHeaderSplitter.java

/**
 * Split./*from   ww w  . j  ava 2 s  .  c o  m*/
 *
 * @param text the text
 * @return the pair
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static Pair<String, Map<String, String>> split(String text) throws IOException {
    LineNumberReader lnr = new LineNumberReader(new StringReader(text));
    Map<String, String> headers = new HashMap<String, String>();
    String line = null;
    boolean leedingNewlines = true;
    while ((line = lnr.readLine()) != null) {
        if (StringUtils.isBlank(line)) {
            if (leedingNewlines == true) {// es kann sein, dass am Anfang die Newlines sind(wegen Code, etc)
                continue;
            } else {
                break;
            }
        }
        String key = StringUtils.substringBefore(line, ":");
        String value = StringUtils.substringAfter(line, ":");
        headers.put(StringUtils.trim(key), StringUtils.trim(value));
        leedingNewlines = false;
    }
    if (headers.size() == 0) {
        return new Pair<String, Map<String, String>>(text, headers);
    }
    return new Pair<String, Map<String, String>>(slurp(lnr), headers);
}

From source file:Main.java

/**
 * Loads an index (Hashtable) from a file.
 * /* w  w  w  .  j  av a2s.c o m*/
 * @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:eu.fbk.utils.lsa.util.Anvur.java

static List<String[]> readText(File f) throws IOException {
    logger.info("reading text: " + f + "...");
    List<String[]> list = new ArrayList<String[]>();
    //LineNumberReader lnr = new LineNumberReader(new FileReader(f));
    LineNumberReader lnr = new LineNumberReader(new InputStreamReader(new FileInputStream(f), "UTF-8"));

    String line = null;//from   www. j  a v  a2  s .co m
    int count = 1;
    while ((line = lnr.readLine()) != null) {
        String[] s = StringEscapeUtils.unescapeHtml(line).split("\t");
        list.add(s);
    } // end while

    logger.info(list.size() + " lines read from in " + f);
    lnr.close();
    return list;
}

From source file:eu.fbk.utils.lsa.util.AnvurDev.java

static List<String[]> readText(File f) throws IOException {
    logger.debug("reading text: " + f + "...");
    List<String[]> list = new ArrayList<String[]>();
    //LineNumberReader lnr = new LineNumberReader(new FileReader(f));
    LineNumberReader lnr = new LineNumberReader(new InputStreamReader(new FileInputStream(f), "UTF-8"));

    String line = null;/*  w  ww . jav a2s  .co  m*/
    int count = 1;
    while ((line = lnr.readLine()) != null) {
        String[] s = StringEscapeUtils.unescapeHtml(line).split("\t");
        list.add(s);
    } // end while

    logger.debug(list.size() + " lines read from in " + f);
    lnr.close();
    return list;
}

From source file:edu.stanford.muse.util.ThunderbirdUtils.java

private static Map<String, String> readUserPrefs(String prefsFile) throws IOException {
    // parse lines like 
    // user_pref("mail.server.server2.directory-rel", "[ProfD]Mail/Local Folders");
    // to create a map of user_pref's
    Map<String, String> map = new LinkedHashMap<String, String>();
    LineNumberReader lnr = new LineNumberReader(new InputStreamReader(new FileInputStream(prefsFile), "UTF-8"));
    while (true) {
        String line = lnr.readLine();
        if (line == null) {
            lnr.close();/* w  ww. j  a v  a2s.co m*/
            break;
        }

        line = line.trim();
        // parse the line. maybe this is better done with regexps
        String startSig = "user_pref(";
        if (line.startsWith(startSig)) {
            line = line.substring(startSig.length());
            int idx = line.indexOf(",");
            if (idx < 0)
                continue; // not expected format, bail out
            String result[] = Util.splitIntoTwo(line, ',');
            String key = result[0].trim(), value = result[1].trim();

            if (!value.endsWith(");"))
                continue; // not expected format, bail out   

            value = value.substring(0, value.length() - ");".length()); // strip out the );
            value = value.trim();

            // now remove quotes if present
            if (key.startsWith("\""))
                key = key.substring(1);
            if (value.startsWith("\""))
                value = value.substring(1);
            if (key.endsWith("\""))
                key = key.substring(0, key.length() - 1);
            if (value.endsWith("\""))
                value = value.substring(0, value.length() - 1);
            map.put(key, value);
        }
    }
    log.info(map.size() + " Thunderbird preferences read from " + prefsFile);
    return map;
}

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');
            }/*from   www .  j  av  a2 s . c o  m*/
            scriptBuilder.append(currentStatement);
        }
        currentStatement = lnr.readLine();
    }
    return scriptBuilder.toString();
}