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:de.micromata.genome.util.text.StandardHeaderSplitter.java

/**
 * Split./*w w  w  .j  a va2 s.  co  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:Transform.java

/**
 * convert a Throwable into an array of Strings
 * @param throwable//from   w ww  .jav a 2s  .  c om
 * @return string representation of the throwable
 */
public static String[] getThrowableStrRep(Throwable throwable) {
    StringWriter sw = new StringWriter();
    PrintWriter pw = new PrintWriter(sw);
    throwable.printStackTrace(pw);
    pw.flush();
    LineNumberReader reader = new LineNumberReader(new StringReader(sw.toString()));
    ArrayList<String> lines = new ArrayList<String>();
    try {
        String line = reader.readLine();
        while (line != null) {
            lines.add(line);
            line = reader.readLine();
        }
    } catch (IOException ex) {
        lines.add(ex.toString());
    }
    String[] rep = new String[lines.size()];
    lines.toArray(rep);
    return rep;
}

From source file:Main.java

public static String getMacAddress() {
    String macAddress = null;/*from w  ww.  j a  v  a 2s . co  m*/
    LineNumberReader reader = null;
    try {
        Process pp = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address");
        InputStreamReader ir = new InputStreamReader(pp.getInputStream());
        reader = new LineNumberReader(ir);
        macAddress = reader.readLine().replace(":", "");
    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        try {
            if (reader != null)
                reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return macAddress == null ? "" : macAddress;
}

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

/**
 *  {@link LineNumberReaderResolver}? {@link Reader}.
 *
 * @param reader//from w  w  w .  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:org.kawanfw.commons.util.FrameworkDebug.java

/**
 * Load the classes to debug from the file
 * // w w  w  . ja  v  a  2s .com
 * @throws IOException
 */
private static void load() {
    if (!CLASSES_TO_DEBUG.isEmpty()) {
        return;
    }

    File kawansoftDir = new File(FrameworkFileUtil.getUserHomeDotKawansoftDir());
    kawansoftDir.mkdirs();

    String file = kawansoftDir + File.separator + KAWANSOFT_DEBUG_INI;

    // Nothing to load if file not set
    if (!new File(file).exists()) {
        CLASSES_TO_DEBUG.add("empty");
        return;
    }

    LineNumberReader lineNumberReader = null;

    try {
        lineNumberReader = new LineNumberReader(new FileReader(file));

        String line = null;
        while ((line = lineNumberReader.readLine()) != null) {
            line = line.trim();

            if (line.startsWith("//") || line.startsWith("#") || line.isEmpty()) {
                continue;
            }

            CLASSES_TO_DEBUG.add(line);
        }
    } catch (FileNotFoundException e) {
        throw new IllegalArgumentException("Wrapped IOException. Impossible to load debug file: " + file, e);
    } catch (IOException e) {
        throw new IllegalArgumentException("Wrapped IOException. Error reading debug file: " + file, e);
    } finally {
        IOUtils.closeQuietly(lineNumberReader);
    }
}

From source file:com.l2jfree.tools.GPLLicenseChecker.java

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

    LineNumberReader lnr = null;
    try {//from ww  w.j  a  v  a2  s . c om
        lnr = new LineNumberReader(new FileReader(f));

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

    // to skip the script classes
    for (String line : list)
        if (line.startsWith("package com.sun.script."))
            return null;

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

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

    return list;
}

From source file:Main.java

public static String getRandomNumByLine(int i) {
    File file = new File("/storage/emulated/0/uber_random");
    LineNumberReader lineNumberReader = null;
    StringBuilder builder = new StringBuilder();
    try {//  ww  w . ja  v a  2s  .  c om
        if (file.exists()) {
            lineNumberReader = new LineNumberReader(new FileReader(file));
            String tmp = null;
            while ((tmp = lineNumberReader.readLine()) != null) {
                builder.append(tmp + "\n");
            }
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (null != lineNumberReader) {
            try {
                lineNumberReader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    String fileString = builder.toString();
    Log.d("TAG", "Read num is --->" + fileString);
    String[] split = fileString.split("\\n");
    if (split != null && (split.length >= i)) {
        String value = split[i];
        Log.d("TAG", "Read sub is --->" + value);
        return value;
    }
    return null;
}

From source file:org.jumpmind.metl.core.runtime.component.XmlReader.java

protected static void forward(int toLine, LineNumberReader lineNumberReader) throws IOException {
    while (lineNumberReader.getLineNumber() < toLine) {
        String output = lineNumberReader.readLine();
        if (output == null) {
            break;
        }//  w  ww.  j  ava 2s  .  c  om
    }
}

From source file:org.kalypso.ogc.sensor.timeseries.wq.at.AtTable.java

/**
 * Reads a .at file from an InpuStream into a WQTableSet.
 *
 * @throws IOException//  w ww  .  j  a v  a  2 s.c o m
 * @throws IOException
 */
private static AtTable readAt(final InputStream is) throws IOException {
    final LineNumberReader reader = new LineNumberReader(new InputStreamReader(is));

    final String firstLine = reader.readLine();
    if (firstLine == null)
        throw new IOException(Messages.getString("org.kalypso.ogc.sensor.timeseries.wq.at.AtTable.1")); //$NON-NLS-1$
    final String secondLine = reader.readLine();
    if (secondLine == null)
        throw new IOException(Messages.getString("org.kalypso.ogc.sensor.timeseries.wq.at.AtTable.2")); //$NON-NLS-1$

    final Matcher matcher = PATTERN_SECOND_LINE.matcher(secondLine);
    if (!matcher.matches())
        throw new IOException(
                Messages.getString("org.kalypso.ogc.sensor.timeseries.wq.at.AtTable.3", secondLine)); //$NON-NLS-1$

    final int size = Integer.parseInt(matcher.group(1));
    final String typeFrom = matcher.group(2);
    final String typeTo = matcher.group(3);

    // TODO: first line ist not parsed yet
    final String name = firstLine;
    final int elbaNr = -1;
    final BigDecimal min = null;
    final BigDecimal max = null;
    final Date validity = null;
    // TODO: get validity from a) the stream b) the atDictionary
    // TODO: discuss with moni?

    final List<WQPair> wqList = new ArrayList<>(size);

    // TODO: read table from stream
    while (reader.ready()) {
        final String line = reader.readLine();
        if (line == null)
            break;

        final String[] strings = line.trim().split("\\s+"); //$NON-NLS-1$
        if (strings.length != 2)
            throw new IOException(Messages.getString("org.kalypso.ogc.sensor.timeseries.wq.at.AtTable.6", //$NON-NLS-1$
                    reader.getLineNumber(), line));

        try {
            final double w = Double.parseDouble(strings[0]);
            final double q = Double.parseDouble(strings[1]);

            wqList.add(new WQPair(w, q));
        } catch (final NumberFormatException e) {
            throw new IOException(Messages.getString("org.kalypso.ogc.sensor.timeseries.wq.at.AtTable.8", //$NON-NLS-1$
                    reader.getLineNumber(), line));
        }
    }

    if (wqList.size() != size) {
        // ignore; maybe produce a warning?
    }

    final WQPair[] pairs = wqList.toArray(new WQPair[wqList.size()]);

    return new AtTable(name, elbaNr, min, max, typeFrom, typeTo, validity, pairs);
}

From source file:com.indoqa.lang.util.StringUtils.java

/**
 * Count the lines of the passed test. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return
 * ('\r'), or a carriage return followed immediately by a linefeed.
 *
 * @param content The text whose lines are to be counted.
 * @return The number of lines.//from   w ww  .j  a  va  2s .  c om
 */
public static int countLines(String content) {
    if (org.apache.commons.lang3.StringUtils.isEmpty(content)) {
        return 0;
    }

    LineNumberReader lnr = new LineNumberReader(new StringReader(content));

    int count = 0;
    try {
        while (lnr.readLine() != null) {
            count++;
        }
    } catch (IOException ioe) {
        return -1;
    }

    return count;
}