Example usage for java.io LineNumberReader close

List of usage examples for java.io LineNumberReader close

Introduction

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

Prototype

public void close() throws IOException 

Source Link

Usage

From source file:com.autentia.tnt.bill.migration.BillToBillPaymentMigration.java

private static void cierraFichero(LineNumberReader f) {
    try {//from w  w  w  .java2  s  .c  o m
        if (f != null) {
            f.close();
        }
    } catch (Exception ex) {
        log.error("Error al cerrar fichero", ex);
    }
}

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  ww  .  j  a  v a  2s . c o  m*/
    String lineRead = "";
    while ((lineRead = reader.readLine()) != null) {
    }
    cnt = reader.getLineNumber();
    reader.close();
    return cnt;
}

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  www.ja  va  2  s .co m*/
        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

public static String getMacAddress() {
    String macAddress = null;/* w w  w. jav a2s  .c o  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:be.jacobsvanroy.springsqlunit.util.ScriptUtils.java

/**
 * Read a script from the provided resource, using the supplied comment prefix
 * and statement separator, and build a {@code String} containing the lines.
 * <p>Lines <em>beginning</em> with the comment prefix are excluded from the
 * results; however, line comments anywhere else &mdash; for example, within
 * a statement &mdash; will be included in the results.
 *
 * @param resource      the {@code EncodedResource} containing the script
 *                      to be processed//w w  w  .j a  v a 2  s . c o  m
 * @param commentPrefix the prefix that identifies comments in the SQL script &mdash;
 *                      typically "--"
 * @param separator     the statement separator in the SQL script &mdash; typically ";"
 * @return a {@code String} containing the script lines
 * @throws IOException in case of I/O errors
 */
private static String readScript(EncodedResource resource, String commentPrefix, String separator)
        throws IOException {
    LineNumberReader lnr = new LineNumberReader(resource.getReader());
    try {
        return readScript(lnr, commentPrefix, separator);
    } finally {
        lnr.close();
    }
}

From source file:org.kalypso.commons.runtime.LogAnalyzer.java

/**
 * Reads a log file into an array of (multi-)statuses.
 * <p>//  w w w .  j av  a 2 s .c  o m
 * The log file is parsed as follows:
 * <ul>
 * <li>All lines formatted as a log-message are parsed into a list of status object. (See {@link LoggerUtilities})</li>
 * <li>All statuses between two messages with code {@link LoggerUtilities#CODE_NEW_MSGBOX}go into a separate
 * MultiStatus.</li>
 * <li>Only messages with a code unequal to {@link LoggerUtilities#CODE_NONE}are considered.</li>
 * <li>The message text of each multi-status will be composed of all its statuses with code
 * {@link LoggerUtilities#CODE_SHOW_MSGBOX}</li>
 * </ul>
 *
 * @return The resulting stati. If problems are encountered while reading the file, instead an error status describing
 *         the io-problems is returned.
 */
public static IStatus[] logfileToStatus(final File file, final String charset) {
    InputStream inputStream = null;
    LineNumberReader lnr = null;
    try {
        inputStream = new FileInputStream(file);
        lnr = new LineNumberReader(new InputStreamReader(inputStream, charset));

        final IStatus[] result = readerToStatus(lnr);

        lnr.close();

        return result;
    } catch (final Throwable t) {
        final StringBuffer msg = new StringBuffer(
                Messages.getString("org.kalypso.commons.runtime.LogAnalyzer.0")); //$NON-NLS-1$
        if (lnr != null) {
            msg.append(Messages.getString("org.kalypso.commons.runtime.LogAnalyzer.1")); //$NON-NLS-1$
            msg.append(lnr.getLineNumber());
        }

        msg.append(Messages.getString("org.kalypso.commons.runtime.LogAnalyzer.2")); //$NON-NLS-1$
        msg.append(file.getAbsolutePath());

        final IStatus status = new Status(IStatus.ERROR, KalypsoCommonsPlugin.getID(),
                LoggerUtilities.CODE_SHOW_MSGBOX, msg.toString(), t);

        return new IStatus[] { status };
    } finally {
        IOUtils.closeQuietly(inputStream);
    }
}

From source file:org.springframework.jdbc.datasource.init.ScriptUtils.java

/**
 * Read a script from the provided resource, using the supplied comment prefix
 * and statement separator, and build a {@code String} containing the lines.
 * <p>Lines <em>beginning</em> with the comment prefix are excluded from the
 * results; however, line comments anywhere else &mdash; for example, within
 * a statement &mdash; will be included in the results.
 * @param resource the {@code EncodedResource} containing the script
 * to be processed//from   ww  w.  j a  va 2 s.c  o m
 * @param commentPrefix the prefix that identifies comments in the SQL script &mdash;
 * typically "--"
 * @param separator the statement separator in the SQL script &mdash; typically ";"
 * @return a {@code String} containing the script lines
 * @throws IOException in case of I/O errors
 */
private static String readScript(EncodedResource resource, @Nullable String commentPrefix,
        @Nullable String separator) throws IOException {

    LineNumberReader lnr = new LineNumberReader(resource.getReader());
    try {
        return readScript(lnr, commentPrefix, separator);
    } finally {
        lnr.close();
    }
}

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  w w w  .j ava 2  s .c  o  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: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 {//from ww w .  j  av a 2  s.c o  m
        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: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;/*from  w ww. j  a va  2 s.  c  o 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;
}