Example usage for com.google.common.io Resources readLines

List of usage examples for com.google.common.io Resources readLines

Introduction

In this page you can find the example usage for com.google.common.io Resources readLines.

Prototype

public static List<String> readLines(URL url, Charset charset) throws IOException 

Source Link

Document

Reads all of the lines from a URL.

Usage

From source file:com.epam.dlab.core.BillingUtils.java

/** Read and return content as string list from resource.
 * @param resourceName the name of resource.
 * @return list of strings./*from   w  w  w  .  j  a va  2 s.  co m*/
 * @throws InitializationException
 */
public static List<String> getResourceAsList(String resourceName) throws InitializationException {
    try {
        URL url = BillingToolConfigurationFactory.class.getResource(resourceName);
        if (url == null) {
            throw new InitializationException("Resource " + resourceName + " not found");
        }
        return Resources.readLines(url, Charset.forName("utf-8"));
    } catch (IllegalArgumentException | IOException e) {
        throw new InitializationException(
                "Cannot read resource " + resourceName + ": " + e.getLocalizedMessage(), e);
    }
}

From source file:fr.loria.parole.artimate.data.io.XWavesSegmentation.java

public void load(String fileName) throws Exception {
    URL url = getClass().getResource("/" + fileName);
    List<String> lines = Resources.readLines(url, Charsets.UTF_8);

    boolean header = true;
    float lastEndTime = 0;
    Matcher matcher = LINE_PATTERN.matcher();
    int index = 0;
    for (String line : lines) {
        // end of header?
        if (line.trim().equals("#")) {
            header = false;//from  w  w w.ja v a 2 s  .c  om
            continue;
        }
        // ignore header line
        if (header) {
            continue;
        }

        // no longer in header, parse line
        if (!matcher.matches(line)) {
            throw new Exception("Could not parse line: " + line);
        }
        String label = matcher.group("label");
        String end = matcher.group("end");

        // sanity check for valid end times
        float endTime = 0;
        try {
            endTime = Float.parseFloat(end);
        } catch (NumberFormatException e) {
            throw new Exception("File not well-formed, could not parse end time: " + end);
        }
        if (endTime < lastEndTime) {
            throw new Exception("Unit end times are not in ascending order!");
        }

        // convert to frame number and append new segment
        Unit segment = new Unit(lastEndTime, endTime, label);
        segment.setIndex(index++);
        units.add(segment);
        lastEndTime = endTime;
    }
    logger.info("Loaded Xwaves lab file " + fileName);
}

From source file:com.mapr.synth.samplers.StreetNameSampler.java

public StreetNameSampler() {
    Splitter onTabs = Splitter.on("\t");
    try {// ww  w.  j a va  2 s .  c  o  m
        for (String line : Resources.readLines(Resources.getResource("street-name-seeds"), Charsets.UTF_8)) {
            if (!line.startsWith("#")) {
                Iterator<Multinomial<String>> i = sampler.iterator();
                for (String name : onTabs.split(line)) {
                    i.next().add(name, 1);
                }
            }
        }
    } catch (IOException e) {
        throw new RuntimeException("Couldn't read built-in resource", e);
    }
}

From source file:com.github.fhirschmann.clozegen.lib.multiset.ReadMultisets.java

/**
 * Parses frequencies from a URL. The subject and the count for a subject
 * need to be delimited by {@code \t} with the count on the right-hand
 * side./*w w w. j  ava 2 s  . co m*/
 *
 * <p>For example, assuming your frequency file contains the following lines:
 * <pre>
 * one of the   200
 * because of the  100
 * members of the   50
 * </pre>
 * Then getting the count of "because of the" will yield 100.
 *
 * @param url the URL to the file to parse
 * @param charset the charset of the file
 * @return the parsed frequencies
 * @throws IOException on errors reading from the file
 */
public static Multiset<String> parseMultiset(final URL url, final Charset charset) throws IOException {
    final Multiset<String> multiset = LinkedHashMultiset.create();
    final List<String> lines = Resources.readLines(checkNotNull(url), charset);

    for (String line : lines) {
        final String[] tokens = line.split("\t");
        multiset.add(tokens[0], Integer.parseInt(tokens[1]));
    }

    return multiset;
}

From source file:com.tdunning.plume.local.eager.LocalPlume.java

@Override
public PCollection<String> readResourceFile(String name) throws IOException {
    return LocalCollection.wrap(Resources.readLines(Resources.getResource(name), Charsets.UTF_8));
}

From source file:com.bennavetta.vetinari.launch.ModuleLoader.java

private List<String> parseListing(URL listingUrl) throws IOException {
    return Resources.readLines(listingUrl, Charsets.UTF_8).stream().map(String::trim)
            .filter(l -> !l.startsWith("#")).filter(l -> !Strings.isNullOrEmpty(l))
            .collect(Collectors.toList());
}

From source file:com.mapr.synth.samplers.StringSampler.java

protected void readDistribution(String resourceName) {
    try {/*from w  ww. j av a 2 s .co m*/
        if (distribution.compareAndSet(null, new Multinomial<String>())) {
            Splitter onTab = Splitter.on("\t").trimResults();
            for (String line : Resources.readLines(Resources.getResource(resourceName), Charsets.UTF_8)) {
                if (!line.startsWith("#")) {
                    Iterator<String> parts = onTab.split(line).iterator();
                    String name = translate(parts.next());
                    double weight = Double.parseDouble(parts.next());
                    distribution.get().add(name, weight);
                }
            }
        }

    } catch (IOException e) {
        throw new RuntimeException("Couldn't read built-in resource file", e);
    }
}

From source file:br.com.objectos.testing.MatcherToStringEqualToResource.java

@Override
protected boolean matchesSafely(T item) {
    try {//from   ww  w  .  java  2s .  com
        List<String> actualLines = readLines(item);
        URL resource = Resources.getResource(item.getClass(), resourceName);
        List<String> expectedLines = Resources.readLines(resource, Charsets.UTF_8);
        lineAssert = LineAssert.get(actualLines, expectedLines);
        return lineAssert.valid();
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}

From source file:org.apache.ctakes.temporal.ae.feature.FramesetCategoryExtractor.java

public FramesetCategoryExtractor() throws ResourceInitializationException {
    String path = "/org/apache/ctakes/temporal/propbank_noneventive_framesets.txt";
    URL uri = FramesetCategoryExtractor.class.getResource(path);
    this.frameSetCategories = Maps.newHashMap();
    try {//from   w  ww.  ja v a  2  s  . c  o  m
        for (String line : Resources.readLines(uri, Charsets.US_ASCII)) {
            String[] tagAndFrameset = line.split("\\s+");
            this.frameSetCategories.put(tagAndFrameset[1], tagAndFrameset[0]);
        }
    } catch (IOException e) {
        throw new ResourceInitializationException(e);
    }
}

From source file:org.apache.ctakes.temporal.ae.feature.TimeWordTypeExtractor.java

public TimeWordTypeExtractor() throws ResourceInitializationException {
    this.wordTypes = Maps.newHashMap();
    URL url = TimeWordsExtractor.class.getResource(LOOKUP_PATH);
    try {//from  w w w .  ja v a2s  . c om
        for (String line : Resources.readLines(url, Charsets.US_ASCII)) {
            String[] typeAndWord = line.split("\\s+");
            if (typeAndWord.length != 2) {
                throw new IllegalArgumentException("Expected '<type> <word>', found: " + line);
            }
            this.wordTypes.put(typeAndWord[1], typeAndWord[0]);
        }
    } catch (IOException e) {
        throw new ResourceInitializationException(e);
    }
}