Example usage for java.lang ClassLoader getResourceAsStream

List of usage examples for java.lang ClassLoader getResourceAsStream

Introduction

In this page you can find the example usage for java.lang ClassLoader getResourceAsStream.

Prototype

public InputStream getResourceAsStream(String name) 

Source Link

Document

Returns an input stream for reading the specified resource.

Usage

From source file:com.msuite.mongodb.application.definition.JSONDefinition.java

public String getJsonFromFile(String fileName) {

    String result = "";

    ClassLoader classLoader = getClass().getClassLoader();
    try {//from   w  ww .j a  v  a2 s . co  m
        result = IOUtils.toString(classLoader.getResourceAsStream(fileName + ".json"));
    } catch (IOException e) {
        e.printStackTrace();
    }

    return result;
}

From source file:com.mycompany.mavenproject1.Ex1.java

private String getFileWithUtil(String fileName) {

    String result = "";

    ClassLoader classLoader = getClass().getClassLoader();
    try {/* w  ww.  j av  a  2s. co  m*/
        result = IOUtils.toString(classLoader.getResourceAsStream(fileName));
    } catch (IOException e) {
        e.printStackTrace();
    }

    return result;
}

From source file:org.ajax4jsf.builder.velocity.ResourceLoader.java

public synchronized InputStream getResourceStream(String name) throws ResourceNotFoundException {
    InputStream result = null;//  w w  w  .j av  a  2 s  . c  o  m

    if (name == null || name.length() == 0) {
        throw new ResourceNotFoundException("No template name provided");
    }

    try {
        ClassLoader classLoader = getClass().getClassLoader();

        result = classLoader.getResourceAsStream(name);
    } catch (Exception fnfe) {
        throw new ResourceNotFoundException(fnfe.getMessage());
    }

    return result;
}

From source file:com.rptools.io.FileParser.java

/**
 * Parses /data/[fileName] into an object T.
 * @param fileName The file to parse.//ww  w .j a  v a  2  s.c o  m
 * @return T Parsed contents of file.
 */
public T parseFile(String fileName) {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    InputStream input = classLoader.getResourceAsStream("/data/" + fileName);
    try {
        String data = IOUtils.toString(input, "UTF-8");
        return parseFileData(data);
    } catch (IOException e) {
        log.error(String.format(PARSE_ERROR, "/data/" + fileName), e);
        return null;
    }
}

From source file:com.wantez.eregparser.Parser.java

public Parser(final String xsl) throws TransformerConfigurationException {
    final TransformerFactory tFactory = TransformerFactory.newInstance();

    final ClassLoader classLoader = this.getClass().getClassLoader();
    final InputStream stream = classLoader.getResourceAsStream(xsl);
    final StreamSource streamSource = new StreamSource(stream);
    streamSource.setSystemId(classLoader.getResource(xsl).getPath());
    this.transformer = tFactory.newTransformer(streamSource);

}

From source file:com.mgmtp.perfload.core.test.client.FibonacciModule.java

@TestData
@Provides//from  ww  w .j  a va2 s  . c o m
Map<String, String> provideTestData() {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    try (InputStream is = cl.getResourceAsStream("testdata/fibonacci.txt")) {
        Map<String, String> result = newHashMapWithExpectedSize(20);
        for (LineIterator it = lineIterator(is, "UTF-8"); it.hasNext();) {
            String line = it.nextLine();
            if (line.startsWith("#")) {
                continue;
            }
            String[] columns = line.split(";");
            result.put(columns[0], columns[1]);
        }
        return result;
    } catch (IOException ex) {
        throw new IllegalStateException("Error reading test data.", ex);
    }
}

From source file:com.mgmtp.perfload.demo.driver.DemoDriverModule.java

/**
 * Reads test data from a file and provides it for dependency injection.
 * //from w w w.j a  v a  2s .co  m
 * @return The test data stored in a list.
 */
@TestData
@Provides
List<String> provideTestData() {
    ClassLoader cl = Thread.currentThread().getContextClassLoader();
    InputStream is = cl.getResourceAsStream("testdata.txt");
    try {
        List<String> result = new ArrayList<>(20);
        for (LineIterator it = lineIterator(is, "UTF-8"); it.hasNext();) {
            String line = it.nextLine();
            if (line.startsWith("#")) {
                continue;
            }
            result.add(line);
        }
        return result;
    } catch (IOException ex) {
        throw new IllegalStateException("Error reading test data.", ex);
    } finally {
        closeQuietly(is);
    }
}

From source file:org.apache.apex.malhar.python.test.BasePythonTest.java

protected void migrateFileFromResourcesFolderToTemp(String resourceFileName, String targetFilePath)
        throws Exception {
    ClassLoader classLoader = getClass().getClassLoader();
    File outFile = new File(targetFilePath);
    FileUtils.copyInputStreamToFile(classLoader.getResourceAsStream(resourceFileName), outFile);
}

From source file:cz.hobrasoft.pdfmu.FileResource.java

public InputStream getStream() {
    ClassLoader classLoader = this.getClass().getClassLoader();
    assert resourceName != null;
    InputStream in = classLoader.getResourceAsStream(resourceName);
    assert in != null;
    return in;//from   w  ww  . j a  v  a  2 s  . c  o m
}

From source file:jfs.sync.encryption.AbstractEncryptedFileProducerFactory.java

public AbstractEncryptedFileProducerFactory() {
    compressionLevels = new HashMap<>();
    Properties p = new Properties();
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    try {//w  w  w  .  j av  a  2s  .  co m
        p.load(classLoader.getResourceAsStream("compression-levels.properties"));
    } catch (Exception e) {
        LOG.error("()", e);
    } // try/catch
    for (Object property : p.keySet()) {
        String extension = "" + property;
        if (!"null".equals(extension)) {
            String limitString = p.getProperty(extension);
            try {
                long limit = Long.parseLong(limitString);
                limit = limit * 1024l * 1024l; // MB
                compressionLevels.put(extension, limit);
            } catch (Exception e) {
                LOG.error("()", e);
            } // try/catch
        } // if
    } // for
}