Example usage for org.apache.commons.io LineIterator close

List of usage examples for org.apache.commons.io LineIterator close

Introduction

In this page you can find the example usage for org.apache.commons.io LineIterator close.

Prototype

public void close() 

Source Link

Document

Closes the underlying Reader quietly.

Usage

From source file:fr.inria.maestro.lga.graph.model.impl.nodenamer.NodeNamerImpl.java

private static int countLines(final File file, final String encoding) throws IOException {
    int count = 0;
    final LineIterator it = FileUtils.lineIterator(file, encoding);
    try {/*from   w  w w .  j  ava 2s .co m*/
        while (it.hasNext()) {
            it.next();
            count++;
        }
    } finally {
        it.close();
    }

    return count;
}

From source file:it.geosolutions.tools.io.file.writer.Writer.java

/**
 * Open 'destination' file in append mode and append content of the
 * 'toAppend' file//from  w ww. j ava2  s  . c  o m
 * 
 * @param toAppend
 * @param destination
 * @throws IOException
 */
public static void appendFile(File toAppend, File destination) throws IOException {
    FileWriter fw = null;
    BufferedWriter bw = null;
    LineIterator it = null;
    try {
        fw = new FileWriter(destination, true);
        bw = new BufferedWriter(fw);
        it = FileUtils.lineIterator(toAppend);
        while (it.hasNext()) {
            bw.append(it.nextLine());
            bw.newLine();
        }
        bw.flush();
    } finally {
        if (it != null) {
            it.close();
        }
        if (bw != null) {
            IOUtils.closeQuietly(bw);
        }
        if (fw != null) {
            IOUtils.closeQuietly(fw);
        }
    }
}

From source file:fr.inria.maestro.lga.graph.model.impl.nodenamer.NodeNamerImpl.java

/**
 * List of lines are generated from a given file. then this list is used to generate a NodeNamerImpl
 *
 * @param file//from w w w  .  j  ava2 s  . c om
 * @return INodeNamer
 * @throws IOException
 */
public static INodeNamer load(final File file) throws IOException {
    final int numNodes = countLines(file, Encoding.DEFAULT_CHARSET_NAME);

    final Int2ObjectOpenHashMap<String> id2Label = new Int2ObjectOpenHashMap<String>(numNodes);
    final Object2IntOpenHashMap<String> label2Id = new Object2IntOpenHashMap<String>(numNodes);

    final LineIterator it = FileUtils.lineIterator(file, Encoding.DEFAULT_CHARSET_NAME);
    try {
        int curId = 0;
        while (it.hasNext()) {
            final String nodeName = it.next();
            id2Label.put(curId, nodeName);
            label2Id.put(nodeName, curId);
            curId++;
        }

        return new NodeNamerImpl(id2Label, label2Id);
    } finally {
        it.close();
    }
}

From source file:com.pieframework.model.repository.ModelStore.java

public static List<ExpressionEntry> parseInstanceFile(File file) throws IOException {
    List<ExpressionEntry> retList = new ArrayList<ExpressionEntry>();
    LineIterator it = FileUtils.lineIterator(file);
    try {//from  w  w w .  j ava 2  s .c o m
        while (it.hasNext()) {
            String line = it.nextLine();
            if (StringUtils.startsWith(line, "#")) {
                // ignore comments
            } else {
                String expression = StringUtils.trimToEmpty(StringUtils.substringBefore(line, "="));
                String value = StringUtils.trimToEmpty(StringUtils.substringAfter(line, "="));
                if (StringUtils.isNotEmpty(expression) && StringUtils.isNotEmpty(value)) {
                    retList.add(new ExpressionEntry().withExpression(expression).withValue(value));
                }
            }
        }
    } finally {
        it.close();
    }
    return retList;
}

From source file:com.fides.Agent.java

private void dumpPropfileContents(File propFile) {
    LineIterator iter = null;
    try {/*from   w ww  .  j  a  va 2 s .  c  o  m*/
        iter = FileUtils.lineIterator(propFile);
        while (iter.hasNext()) {
            logger.debug(iter.next());
        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        if (iter != null)
            iter.close();
    }
}

From source file:com.mewmew.fairy.v1.spell.BaseLineSpell.java

@Override
public void process(InputStream in, OutputStream out) {
    LineIterator iter = null;
    try {//  w  w w  . j av  a 2s.c o  m
        output = createOutput(out);
        pipe = createPipe();
        iter = new LineIterator(new InputStreamReader(in));
        PipeUtil.process(iter, pipe, output);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        if (iter != null) {
            iter.close();
        }
    }
}

From source file:com.shopzilla.hadoop.testing.hdfs.DFSCluster.java

public void processData(final Path path, final Function<String, Void> lineProcessor) throws IOException {
    final Function<Path, Void> pathProcessor = new Function<Path, Void>() {
        @Override//from ww w .j  av a2  s .  c om
        public Void apply(Path path) {
            try {
                final FSDataInputStream in = miniDFSCluster.getFileSystem().open(path);
                final LineIterator lineIterator = new LineIterator(new InputStreamReader(in));
                while (lineIterator.hasNext()) {
                    lineProcessor.apply(lineIterator.next());
                }
                lineIterator.close();
                return null;
            } catch (final Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    };
    processPaths(path, new Function<Path, Void>() {
        @Override
        public Void apply(Path input) {
            pathProcessor.apply(input);
            return null;
        }
    });
}

From source file:com.shopzilla.hadoop.testing.hdfs.DFSCluster.java

public void processDataRecursive(final Path path, final Function<String, Void> lineProcessor)
        throws IOException {
    final Function<Path, Void> pathProcessor = new Function<Path, Void>() {
        @Override//from   ww w.  j a va 2s. co m
        public Void apply(Path path) {
            try {
                final FSDataInputStream in = miniDFSCluster.getFileSystem().open(path);
                final LineIterator lineIterator = new LineIterator(new InputStreamReader(in));
                while (lineIterator.hasNext()) {
                    lineProcessor.apply(lineIterator.next());
                }
                lineIterator.close();
                return null;
            } catch (final Exception ex) {
                throw new RuntimeException(ex);
            }
        }
    };
    processPathsRecursive(path, new Function<Path, Void>() {
        @Override
        public Void apply(Path input) {
            pathProcessor.apply(input);
            return null;
        }
    });
}

From source file:com.shopzilla.hadoop.mapreduce.MiniMRClusterContext.java

public void processData(final Path path, final Function<String, Void> lineProcessor) throws IOException {
    final Function<Path, Void> pathProcessor = new Function<Path, Void>() {
        @Override//from   w  w w . j av  a2 s . c  o m
        public Void apply(Path path) {
            try {
                FSDataInputStream in = miniDFSCluster.getFileSystem().open(path);
                LineIterator lineIterator = new LineIterator(new InputStreamReader(in));
                while (lineIterator.hasNext()) {
                    lineProcessor.apply(lineIterator.next());
                }
                lineIterator.close();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
            return null;
        }
    };
    processPaths(path, new Function<Path, Void>() {
        @Override
        public Void apply(Path input) {
            pathProcessor.apply(input);
            return null;
        }
    });
}

From source file:com.shopzilla.hadoop.mapreduce.MiniMRClusterContext.java

public void processDataRecursive(final Path path, final Function<String, Void> lineProcessor)
        throws IOException {
    final Function<Path, Void> pathProcessor = new Function<Path, Void>() {
        @Override//from www.j a v a2 s. c  o m
        public Void apply(Path path) {
            try {
                FSDataInputStream in = miniDFSCluster.getFileSystem().open(path);
                LineIterator lineIterator = new LineIterator(new InputStreamReader(in));
                while (lineIterator.hasNext()) {
                    lineProcessor.apply(lineIterator.next());
                }
                lineIterator.close();
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
            return null;
        }
    };
    processPathsRecursive(path, new Function<Path, Void>() {
        @Override
        public Void apply(Path input) {
            pathProcessor.apply(input);
            return null;
        }
    });
}