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

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

Introduction

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

Prototype

public boolean hasNext() 

Source Link

Document

Indicates whether the Reader has more lines.

Usage

From source file:org.apache.tez.history.ATSImportTool.java

private void logErrorMessage(ClientResponse response) throws IOException {
    LOG.error("Response status={}", response.getClientResponseStatus().toString());
    LineIterator it = null;
    try {//from w w w.java2 s . co  m
        it = IOUtils.lineIterator(response.getEntityInputStream(), UTF8);
        while (it.hasNext()) {
            String line = it.nextLine();
            LOG.error(line);
        }
    } finally {
        if (it != null) {
            it.close();
        }
    }
}

From source file:org.apache.tez.history.ATSImportTool_V2.java

private String logErrorMessage(ClientResponse response) throws IOException {
    StringBuilder sb = new StringBuilder();
    LOG.error("Response status={}", response.getClientResponseStatus().toString());
    LineIterator it = null;
    try {/*w w w. ja  va2s  .c  om*/
        it = IOUtils.lineIterator(response.getEntityInputStream(), UTF8);
        while (it.hasNext()) {
            String line = it.nextLine();
            LOG.error(line);
        }
    } finally {
        if (it != null) {
            it.close();
        }
    }
    return sb.toString();
}

From source file:org.archive.crawler.migrate.MigrateH1to3Tool.java

protected Map<String, String> getMigrateMap() throws IOException {
    Map<String, String> map = new HashMap<String, String>();
    InputStream inStream = getClass().getResourceAsStream("/org/archive/crawler/migrate/H1toH3.map");
    LineIterator iter = IOUtils.lineIterator(inStream, "UTF-8");
    while (iter.hasNext()) {
        String[] fields = iter.nextLine().split("\\|");
        map.put(fields[1], fields[0]);//from   www  . j  ava2 s .c om
    }
    inStream.close();
    return map;
}

From source file:org.asqatasun.referential.creator.CodeGeneratorMojo.java

/**
 *
 * @return/*from   w ww. j  av a  2s  .c  o  m*/
 */
private Iterable<CSVRecord> getCsv() {
    // we parse the csv file to extract the first line and get the headers 
    LineIterator lineIterator;
    try {
        lineIterator = FileUtils.lineIterator(dataFile, Charset.defaultCharset().name());
    } catch (IOException ex) {
        Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex);
        lineIterator = null;
    }
    String[] csvHeaders = lineIterator != null ? lineIterator.next().split(String.valueOf(delimiter))
            : new String[0];
    isCriterionPresent = extractCriterionFromCsvHeader(csvHeaders);
    try {
        extractAvailableLangsFromCsvHeader(csvHeaders);
    } catch (I18NLanguageNotFoundException ex) {
        Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }

    // from here we just add each line to a build to re-create the csv content
    // without the first line.
    StringBuilder strb = new StringBuilder();
    while (lineIterator.hasNext()) {
        strb.append(lineIterator.next());
        strb.append("\n");
    }
    Reader in;
    try {
        in = new StringReader(strb.toString());
        CSVFormat csvf = CSVFormat.newFormat(delimiter).withHeader(csvHeaders);
        return csvf.parse(in);
    } catch (FileNotFoundException ex) {
        Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    } catch (IOException ex) {
        Logger.getLogger(CodeGeneratorMojo.class.getName()).log(Level.SEVERE, null, ex);
        return null;
    }
}

From source file:org.asqatasun.rules.doc.utils.exportaw22torgaa3ruledesign.ExtractCsvAndCopy.java

public Iterable<CSVRecord> getCsv() throws IOException {
    // we parse the csv file to extract the first line and get the headers 
    LineIterator lineIterator;
    lineIterator = FileUtils.lineIterator(DATA_FILE);
    csvHeaders = lineIterator.next().split(String.valueOf(DELIMITER));

    StringBuilder strb = new StringBuilder();
    while (lineIterator.hasNext()) {
        strb.append(lineIterator.next());
        strb.append("\n");
    }// www . j  a va2s. c o  m

    Reader in;
    try {
        in = new StringReader(strb.toString());
        CSVFormat csvf = CSVFormat.newFormat(DELIMITER).withHeader(csvHeaders);
        return csvf.parse(in);
    } catch (FileNotFoundException ex) {
        return null;
    } catch (IOException ex) {
        return null;
    }
}

From source file:org.bdval.modelselection.CandidateModelSelection.java

private ObjectList<CustomRanking> parseRankings(final String customRankingFilename) {
    try {/*w  ww  . j  av a  2 s  .c  o  m*/
        final ObjectList<CustomRanking> result = new ObjectArrayList<CustomRanking>();
        if (customRankingFilename == null) {
            return result;
        }
        final LineIterator lineIt = new LineIterator(new FileReader(customRankingFilename));
        while (lineIt.hasNext()) {
            final String line = lineIt.nextLine();
            final String[] tokens = line.split("[\t]");
            final CustomRanking ranking = new CustomRanking();
            ranking.datasetName = tokens[0];
            ranking.endpointCode = tokens[1];
            ranking.typeOfRanking = tokens[2];
            final String customRanking = tokens[3];
            ranking.modelIds = new ObjectArrayList<String>(customRanking.split("[,]"));
            result.add(ranking);
        }
        return result;
    } catch (FileNotFoundException e) {
        System.out.println("Cannot open ranking set file  " + customRankingFilename);
        e.printStackTrace();
        System.exit(10);
    }
    return null;

}

From source file:org.bdval.WriteModel.java

@Override
public void interpretArguments(final JSAP jsap, final JSAPResult result, final DAVOptions options) {
    super.interpretArguments(jsap, result, options);
    modelPrefix = result.contains("model-prefix") ? result.getString("model-prefix") : null;
    final Properties extraParameters = new Properties();
    if (result.contains("use-parameters")) {
        final String filename = result.getString("use-parameters");
        try {/* w w w .  java  2  s.c  om*/
            extraParameters.load(filename);
            final ObjectList<String> allParameters = new ObjectArrayList<String>(options.classifierParameters);
            final Iterator<String> parameterKeyIt = extraParameters.getKeys();
            while (parameterKeyIt.hasNext()) {
                final String parameterName = parameterKeyIt.next();
                final double value = extraParameters.getDouble(parameterName);
                final String added = parameterName + "=" + Double.toString(value);
                allParameters.add(added);
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Adding parameter from property file: " + added);
                }
            }
            options.classifierParameters = allParameters.toArray(new String[allParameters.size()]);

        } catch (ConfigurationException e) {
            LOG.error("Cannot load model parameters from file " + filename);
        }
    }
    if (result.contains("consensus-of-models")) {
        final String fileName = result.getString("consensus-of-models");
        Reader reader = null;
        try {
            final ArrayList<String> list = new ArrayList<String>();
            reader = new FileReader(fileName);
            final LineIterator it = new LineIterator(reader);
            while (it.hasNext()) {
                final String line = (String) it.next();
                list.add(line);
            }
            modelComponentPrefixes = list.toArray(new String[list.size()]);

        } catch (FileNotFoundException e) {
            LOG.error("Cannot find list of model components for consensus model: " + fileName);
        } finally {
            IOUtils.closeQuietly(reader);
        }
    }
}

From source file:org.broadinstitute.gatk.utils.io.IOUtils.java

/**
 * Returns the last lines of the file./*from  w w w .ja  va  2  s. com*/
 * NOTE: This is only safe to run on smaller files!
 *
 * @param file  File to read.
 * @param count Maximum number of lines to return.
 * @return The last count lines from file.
 * @throws IOException When unable to read the file.
 */
public static List<String> tail(File file, int count) throws IOException {
    LinkedList<String> tailLines = new LinkedList<String>();
    FileReader reader = new FileReader(file);
    try {
        LineIterator iterator = org.apache.commons.io.IOUtils.lineIterator(reader);
        int lineCount = 0;
        while (iterator.hasNext()) {
            String line = iterator.nextLine();
            lineCount++;
            if (lineCount > count)
                tailLines.removeFirst();
            tailLines.offer(line);
        }
    } finally {
        org.apache.commons.io.IOUtils.closeQuietly(reader);
    }
    return tailLines;
}

From source file:org.carewebframework.maven.plugin.core.ConfigTemplate.java

/**
 * Creates a config file template from a classpath resource.
 * //from   w  ww  .  ja va  2 s. c o m
 * @param filename Path to config file template.
 * @throws MojoExecutionException Error loading the template.
 */
ConfigTemplate(String filename) throws MojoExecutionException {
    this.filename = filename;
    InputStream in = getClass().getResourceAsStream("/" + filename);

    if (in == null) {
        throw new MojoExecutionException("Cannot find config file template: " + filename);
    }

    try {
        LineIterator lines = IOUtils.lineIterator(in, "UTF-8");

        while (lines.hasNext()) {
            String line = lines.next();

            if (line.startsWith("*")) {
                String[] pcs = line.split("\\*", 3);
                entries.put(pcs[1], new ConfigEntry(pcs[2], buffer.size()));
                buffer.add("");
            } else {
                buffer.add(line);
            }
        }
    } catch (Exception e) {
        throw new MojoExecutionException("Unexpected error while creating configuration file.", e);
    } finally {
        IOUtils.closeQuietly(in);
    }

    if (entries.isEmpty()) {
        throw new MojoExecutionException("Failed to locate insertion point in configuration file.");
    }
}

From source file:org.carewebframework.maven.plugin.theme.CSSTransform.java

@Override
public void process(IResource resource, OutputStream outputStream) throws Exception {
    CSSResource css = (CSSResource) resource;
    File mapper = css.getMapper();

    if (mapper == null) {
        super.process(resource, outputStream);
        return;/*from   w  ww.jav  a 2 s. com*/
    }
    InputStream in = new FileInputStream(mapper);
    LineIterator lines = IOUtils.lineIterator(in, "UTF-8");
    String line = "";

    while (lines.hasNext()) {
        line += lines.nextLine();

        if (line.endsWith("\\")) {
            line = StringUtils.left(line, line.length() - 1);
        } else {
            addMapEntry(line);
            line = "";
        }
    }

    addMapEntry(line);
    IOUtils.closeQuietly(in);
    super.process(resource, outputStream);
}