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:org.apache.pig.scripting.Pig.java

private static String getScriptFromFile(String filename) throws IOException {
    LineNumberReader rd = new LineNumberReader(new FileReader(filename));
    StringBuilder sb = new StringBuilder();
    try {/*from w  w  w  .  j a va 2 s  . c  o m*/
        String line = rd.readLine();
        while (line != null) {
            sb.append(line);
            sb.append("\n");
            line = rd.readLine();
        }
    } finally {
        rd.close();
    }
    return sb.toString();
}

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();
            break;
        }/* w ww.  j  a v  a  2 s . co  m*/

        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.termmed.utils.FileHelper.java

/**
 * Count lines.//from w w w  . j av  a 2  s .  c  om
 *
 * @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;
    }
}

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  . ja  v a2 s  .c  o 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:net.sqs2.util.FileUtil.java

/**
 * keyword replace in stream (replacing string cannot contain newline
 * chars)./*from  w  ww .java2 s . c o m*/
 * 
 * @param inputStream
 * @param outputStream
 * @param from
 *            keyword(from)
 * @param to
 *            keyword(to)
 * @param encoding
 *            file encoding
 * @return true: some keywords replaced, false: no keyword replaced
 */
public static boolean keywordSubstitution(InputStream inputStream, OutputStream outputStream, String from,
        String to, String encoding) {
    boolean modified = false;
    try {
        PrintWriter writer = new PrintWriter(
                new OutputStreamWriter(new BufferedOutputStream(outputStream), encoding));
        LineNumberReader reader = new LineNumberReader(
                new InputStreamReader(new BufferedInputStream(inputStream), encoding));
        String line = null;
        while ((line = reader.readLine()) != null) {
            String result = StringUtil.replaceAll(line, from, to);
            writer.println(result);
            if (!result.equals(line)) {
                modified |= true;
            }
        }
        reader.close();
        writer.close();
    } catch (IOException e) {
        e.printStackTrace();
        new RuntimeException(e);
    }
    return modified;
}

From source file:hobby.wei.c.phone.Network.java

public static String DNS(int n, boolean format) {
    String dns = null;/*from   ww  w.  ja  v a2s  . co  m*/
    Process process = null;
    LineNumberReader reader = null;
    try {
        final String CMD = "getprop net.dns" + (n <= 1 ? 1 : 2);

        process = Runtime.getRuntime().exec(CMD);
        reader = new LineNumberReader(new InputStreamReader(process.getInputStream()));

        String line = null;
        while ((line = reader.readLine()) != null) {
            dns = line.trim();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            if (reader != null)
                reader.close();
            if (process != null)
                process.destroy(); //??
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return format ? (dns != null ? dns.replace('.', '_') : dns) : dns;
}

From source file:nl.uva.illc.dataselection.InvitationModel.java

public static void readFiles() throws IOException, InterruptedException {

    log.info("Reading files");

    src_codes = HashObjIntMaps.newMutableMap();
    trg_codes = HashObjIntMaps.newMutableMap();
    src_codes.put(null, 0);/* w w  w  . j a  v  a 2s . c om*/
    trg_codes.put(null, 0);

    LineNumberReader lr = new LineNumberReader(new FileReader(IN + "." + SRC));
    lr.skip(Long.MAX_VALUE);
    int indomain_size = lr.getLineNumber();
    lr.close();

    lr = new LineNumberReader(new FileReader(MIX + "." + SRC));
    lr.skip(Long.MAX_VALUE);
    int mixdomain_size = lr.getLineNumber();
    lr.close();

    src_indomain = new int[indomain_size][];
    trg_indomain = new int[indomain_size][];
    src_mixdomain = new int[mixdomain_size][];
    trg_mixdomain = new int[mixdomain_size][];

    latch = new CountDownLatch(2);
    readFile(IN + "." + SRC, src_codes, src_indomain);
    readFile(IN + "." + TRG, trg_codes, trg_indomain);
    latch.await();

    latch = new CountDownLatch(2);
    readFile(MIX + "." + SRC, src_codes, src_mixdomain);
    readFile(MIX + "." + TRG, trg_codes, trg_mixdomain);
    latch.await();

}

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

/**
 * Copy to./*from  w  w  w .  j a va2s.  c o  m*/
 *
 * @param inputFile the input file
 * @param outputFile the output file
 * @throws IOException Signals that an I/O exception has occurred.
 */
public static void copyTo(File inputFile, File outputFile) throws IOException {

    FileInputStream fis = new FileInputStream(inputFile);
    InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
    LineNumberReader reader = new LineNumberReader(isr);

    FileOutputStream fos = new FileOutputStream(outputFile);
    OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
    BufferedWriter bw = new BufferedWriter(osw);

    String lineRead = "";
    while ((lineRead = reader.readLine()) != null) {
        bw.append(lineRead);
        bw.append("\r\n");
    }
    reader.close();
    bw.close();

}

From source file:com.sds.acube.ndisc.xmigration.util.XNDiscMigUtil.java

/**
 * XMigration ? ?//ww  w  .j a  va2 s .c om
 */
private static void readVersionFromFile() {
    XMigration_PublishingVersion = "<unknown>";
    XMigration_PublishingDate = "<unknown>";
    InputStreamReader isr = null;
    LineNumberReader lnr = null;
    try {
        isr = new InputStreamReader(
                XNDiscMigUtil.class.getResourceAsStream("/com/sds/acube/ndisc/xmigration/version.txt"));
        if (isr != null) {
            lnr = new LineNumberReader(isr);
            String line = null;
            do {
                line = lnr.readLine();
                if (line != null) {
                    if (line.startsWith("Publishing-Version=")) {
                        XMigration_PublishingVersion = line
                                .substring("Publishing-Version=".length(), line.length()).trim();
                    } else if (line.startsWith("Publishing-Date=")) {
                        XMigration_PublishingDate = line.substring("Publishing-Date=".length(), line.length())
                                .trim();
                    }
                }
            } while (line != null);
            lnr.close();
        }
    } catch (IOException ioe) {
        XMigration_PublishingVersion = "<unknown>";
        XMigration_PublishingDate = "<unknown>";
    } finally {
        try {
            if (lnr != null) {
                lnr.close();
            }
            if (isr != null) {
                isr.close();
            }
        } catch (IOException ioe) {
        }
    }
}

From source file:pl.nask.hsn2.service.FileFeederTask.java

private void closeReader(LineNumberReader reader) {
    try {//w w w . j  a v a  2s .  c o m
        if (reader != null) {
            reader.close();
        }
    } catch (IOException e) {
        LOG.warn("Error closing file", e);
    }
}