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:com.googlecode.jsonschema2pojo.integration.util.FileSearchMatcher.java

private boolean isSearchTextPresentInLinesOfFile(File f) {
    LineIterator it = null;
    try {/*from  w w w.ja  v  a2s.c o  m*/
        it = FileUtils.lineIterator(f, "UTF-8");
        while (it.hasNext()) {
            String line = it.nextLine();
            if (line.contains(searchText)) {
                return true;
            }
        }
        return false;
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        LineIterator.closeQuietly(it);
    }
}

From source file:eu.eexcess.domaindetection.wordnet.XwndReader.java

public void read(File file) throws IOException {
    String domain = FilenameUtils.getBaseName(file.getName());

    File cacheFile = new File(file.getPath() + ".cache");
    if (!cacheFile.exists()) {
        BinaryOutputStream bos = new BinaryOutputStream(new FileOutputStream(cacheFile));
        System.out.println("Read in the Extended WordNet Domains file: " + file);
        LineIterator iterator = new LineIterator(new FileReader(file));
        while (iterator.hasNext()) {
            String line = iterator.nextLine();
            String[] tokens = line.split("\t");
            String synset = tokens[0];
            double weight = Double.parseDouble(tokens[1]);
            String[] ssid = synset.split("-");
            int nr = Integer.parseInt(ssid[0]);
            POS pos = POS.getPOSForKey(ssid[1]);
            bos.writeInt(nr);// w w w  . jav  a2s  .c  o  m
            bos.writeSmallInt(pos.getId());
            bos.writeInt(Float.floatToIntBits((float) weight));
        }
        iterator.close();
        bos.close();
    }

    System.out.println("Read in the Extended WordNet Domains cache file: " + file);
    FileInputStream fStream = new FileInputStream(cacheFile);
    BinaryInputStream bis = new BinaryInputStream(fStream);
    while (bis.available() > 0) {
        int nr = bis.readInt();
        int key = bis.readSmallInt();
        POS pos = POS.getPOSForId(key);
        String synset = String.format("%08d-%s", nr, pos.getKey());
        double weight = Float.intBitsToFloat(bis.readInt());
        DomainAssignment assignment = new DomainAssignment(domain, weight);
        Set<DomainAssignment> domains = synsetToDomains.get(synset);
        if (domains == null) {
            domains = new TreeSet<DomainAssignment>();
            synsetToDomains.put(synset, domains);
        }
        domains.add(assignment);
    }
    fStream.close();
    bis.close();
}

From source file:com.icantrap.collections.dawg.TrieValidationTest.java

@Before
public void before() throws IOException {
    assumeThat(System.getProperty("RUN_VALIDATION"), is("on"));
    LineIterator iter = IOUtils.lineIterator(getClass().getResourceAsStream("/TWL06.txt"), null);
    dawgBuilder = new DawgBuilder();

    while (iter.hasNext())
        dawgBuilder.add(iter.next());/*  ww w. ja  v  a2  s .  co m*/

    LineIterator.closeQuietly(iter);

    System.out.println("Uncompressed:  " + dawgBuilder.nodeCount() + " nodes");

    StopWatch stopWatch = new StopWatch();
    stopWatch.start();
    dawgBuilder.build();
    stopWatch.stop();

    System.out.println("Time to compress:  " + stopWatch.getTime() + " ms.");
    System.out.println("Compressed:  " + dawgBuilder.nodeCount() + " nodes");
}

From source file:net.pms.io.OutputTextConsumer.java

public void run() {
    LineIterator it = null;

    try {//from   w  w  w  . j av a2 s  . c  o m
        it = IOUtils.lineIterator(inputStream, "UTF-8");

        while (it.hasNext()) {
            String line = it.nextLine();

            if (line.length() > 0) {
                addLine(line);
            }

            if (log) {
                logger.debug(line);
            }
        }
    } catch (IOException ioe) {
        logger.debug("Error consuming input stream: {}", ioe.getMessage());
    } catch (IllegalStateException ise) {
        logger.debug("Error reading from closed input stream: {}", ise.getMessage());
    } finally {
        LineIterator.closeQuietly(it); // clean up all associated resources
    }
}

From source file:com.fides.Agent.java

private void dumpPropfileContents(File propFile) {
    LineIterator iter = null;
    try {//from   w w  w.  j av a2  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:cn.org.once.cstack.maven.plugin.handler.ResponseErrorHandler.java

@Override
public String handleResponse(HttpResponse response) throws ClientProtocolException, IOException {

    int status = response.getStatusLine().getStatusCode();

    if (status >= 200 && status < 300) {
        HttpEntity entity = response.getEntity();
        return entity != null ? EntityUtils.toString(entity) : null;
    } else {//from  w  ww.j a v a 2s. c om
        switch (status) {
        case 500:
            InputStreamReader reader = null;
            reader = new InputStreamReader(response.getEntity().getContent());
            LineIterator lineIterator = new LineIterator(reader);
            StringBuilder jsonStringBuilder = new StringBuilder();

            while (lineIterator.hasNext()) {
                jsonStringBuilder.append(lineIterator.nextLine());
            }
            throw new ClientProtocolException(jsonStringBuilder.toString());
        case 401:
            throw new ClientProtocolException("Status 401 - Bad credentials!");
        case 403:
            throw new ClientProtocolException("Status 403 - You must be an admin to execute this command!");
        case 404:
            throw new ClientProtocolException(
                    "Status 404 - The server can treat the request, please contact an admin");
        default:
            throw new ClientProtocolException("Cloudunit server does not response. Please contact an admin");
        }
    }

}

From source file:com.zenika.doclipser.api.DockerClientJavaApi.java

@Override
public void defaultBuildCommand(String eclipseProjectName, String dockerBuildContext) {
    File baseDir = new File(dockerBuildContext);
    InputStream response = dockerClient.buildImageCmd(baseDir).exec();
    StringWriter logwriter = new StringWriter();
    messageConsole.getDockerConsoleOut()
            .println(">>> Building " + dockerBuildContext + "/Dockerfile with default options");
    messageConsole.getDockerConsoleOut().println("");

    try {/*w  w w .  j  a  v  a  2  s.c  o  m*/
        messageConsole.getDockerConsoleOut().flush();
        LineIterator itr = IOUtils.lineIterator(response, "UTF-8");
        while (itr.hasNext()) {
            String line = itr.next();
            logwriter.write(line);
            messageConsole.getDockerConsoleOut().println(line);
            messageConsole.getDockerConsoleOut().flush();
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        IOUtils.closeQuietly(response);
    }

    messageConsole.getDockerConsoleOut().println("");
    messageConsole.getDockerConsoleOut().println("<<< Build ended");
}

From source file:modelinspector.collectors.MostFrequentWordsCollector.java

public MostFrequentWordsCollector(String aLanguage, int aCutoff, boolean aCaseSensitive, String aFile,
        String aEncoding) {/*from   w w  w . j  a v  a 2  s  .  c  o m*/
    cutoff = aCutoff;
    language = new Locale(aLanguage);

    String key = aLanguage + "-" + aCaseSensitive;

    setShowSample(true);

    wordList = wordLists.get(key);
    if (wordList == null) {
        wordList = new Object2IntOpenHashMap<>();
        // The file read is sorted by frequency
        try (InputStream is = new FileInputStream(aFile)) {
            LineIterator i = IOUtils.lineIterator(is, aEncoding);
            int n = 1;
            while (i.hasNext()) {
                String[] fields = i.nextLine().split("\t");
                String word = aCaseSensitive ? fields[0] : fields[0].toLowerCase(language);
                // System.out.println(word + " - " + n);
                // Record the word and its rank - since a word may appear in different
                // frequencies with different POSes, we need to make sure we don't overwrite
                // a frequent POS with an infrequent one. We only consider the most frequent
                // POS for a word.
                if (!wordList.containsKey(word)) {
                    wordList.put(word, n);
                }
                n++;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        wordList.defaultReturnValue(Integer.MAX_VALUE);
        wordLists.put(key, wordList);
    }
}

From source file:de.tudarmstadt.lt.lm.service.LtSegProvider.java

@Override
public List<String> splitSentences(String text, String language_code) throws Exception {
    LOG.trace(String.format("Splitting sentences from text: %s", StringUtils.abbreviate(text, 200)));
    List<String> sentences = new ArrayList<String>();

    if (Properties.onedocperline()) {
        LineIterator liter = new LineIterator(new StringReader(text));
        for (String line; (line = liter.hasNext() ? liter.next() : null) != null;)
            split_and_add_sentences(line, sentences);
    } else {/*from w w  w. j  a va  2s. c om*/
        split_and_add_sentences(text, sentences);
    }

    LOG.trace(String.format("Split text '%s' into '%d' sentences.", StringUtils.abbreviate(text, 200),
            sentences.size()));
    return sentences;
}

From source file:com.icantrap.collections.dawg.TrieValidationTest.java

@Test
public void containsAllWords() throws IOException {
    LineIterator iter = IOUtils.lineIterator(getClass().getResourceAsStream("/TWL06.txt"), null);

    StopWatch stopWatch = new StopWatch();
    stopWatch.start();/*from www .j  a  va 2 s  . c o m*/

    while (iter.hasNext()) {
        String word = iter.next();
        assertTrue("Missing word (" + word + ")", dawgBuilder.contains(word));
    }

    stopWatch.stop();
    System.out.println("Time to query:  " + stopWatch.getTime() + " ms.");

    LineIterator.closeQuietly(iter);
}