Example usage for java.lang ClassLoader getSystemResourceAsStream

List of usage examples for java.lang ClassLoader getSystemResourceAsStream

Introduction

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

Prototype

public static InputStream getSystemResourceAsStream(String name) 

Source Link

Document

Open for reading, a resource of the specified name from the search path used to load classes.

Usage

From source file:Main.java

/**
 * //from   w w w.j a v a2s .co m
 * @param filePath
 * @return
 */
public static InputStream getXMLFile(String filePath) {
    return ClassLoader.getSystemResourceAsStream(filePath);
}

From source file:Main.java

public static org.w3c.dom.Document configureJDomDocFromResource(String fileName) {
    org.w3c.dom.Document doc = null;
    InputStream in = ClassLoader.getSystemResourceAsStream(fileName);

    if (in != null) {
        try {/*  ww w .  j  av  a2  s .c o  m*/
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            factory.setValidating(false);
            factory.setNamespaceAware(false);
            doc = factory.newDocumentBuilder().parse(in);
        } catch (Exception e) {
            //                Debug.error(e);
        }
    } else {
        //            Debug.error("could not find file " + fileName);
    }

    return doc;
}

From source file:Main.java

public static String readFileAsString(String resource, boolean keepLineBreaks) throws IOException {
    BufferedReader reader = new BufferedReader(
            new InputStreamReader(ClassLoader.getSystemResourceAsStream("resources/" + resource)));
    StringBuffer resultString = new StringBuffer();
    String line = null;/*  w w w. j  a va  2s  .co  m*/
    while ((line = reader.readLine()) != null) {
        resultString.append(line);
        if (keepLineBreaks) {
            resultString.append("\n");
        }
    }
    return resultString.toString();
}

From source file:Main.java

public static Document file2Document(String filename)
        throws ParserConfigurationException, SAXException, IOException {
    InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(filename);
    if (is == null) {
        is = ClassLoader.getSystemResourceAsStream(filename);
    }//w  w w . j av a2  s .c o m

    if (is == null) {
        return null;
    }
    return inputStream2Document(is);
}

From source file:com.fluke.database.DatabaseProperty.java

public static DataSource getDataSource() {
    if (source != null) {
        return source;
    }/*from w  ww  . ja  v  a 2 s . co m*/
    Properties props = new Properties();
    FileInputStream fis = null;

    BasicDataSource dataSource = new BasicDataSource();
    try {

        props.load(ClassLoader.getSystemResourceAsStream("config/mysql.properties"));
        dataSource.setDriverClassName(props.getProperty("MYSQL_DB_DRIVER_CLASS"));
        dataSource.setUrl(props.getProperty("MYSQL_DB_URL"));
        dataSource.setUsername(props.getProperty("MYSQL_DB_USERNAME"));
        dataSource.setPassword(props.getProperty("MYSQL_DB_PASSWORD"));

    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return source = dataSource;
}

From source file:com.rhino.data.db.DataSourceFactory.java

public static DataSource getDataSource() {
    if (source != null) {
        return source;
    }/*from w ww  .j a v a 2 s.  com*/
    Properties props = new Properties();
    FileInputStream fis = null;

    BasicDataSource dataSource = new BasicDataSource();
    try {

        props.load(ClassLoader.getSystemResourceAsStream("mysql.properties"));
        dataSource.setDriverClassName(props.getProperty("MYSQL_DB_DRIVER_CLASS"));
        dataSource.setUrl(props.getProperty("MYSQL_DB_URL"));
        dataSource.setUsername(props.getProperty("MYSQL_DB_USERNAME"));
        dataSource.setPassword(props.getProperty("MYSQL_DB_PASSWORD"));

    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return source = dataSource;
}

From source file:org.apache.james.util.ClassLoaderUtils.java

public static String getSystemResourceAsString(String filename, Charset charset) {
    try {/*w  w w  . jav  a2s  .  c om*/
        return IOUtils.toString(ClassLoader.getSystemResourceAsStream(filename), charset);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:com.siemens.sw360.licenseinfo.TestHelper.java

public static InputStream makeAttachmentContentStream(String filename) {
    return ClassLoader.getSystemResourceAsStream(filename);
}

From source file:uk.ac.kcl.it.MagicSquare.java

@PostConstruct
public void readFile() throws IOException {
    // If included in an Eclipse project.
    InputStream stream = ClassLoader.getSystemResourceAsStream("magic-square.csv");
    BufferedReader buffer = new BufferedReader(new InputStreamReader(stream));

    // If in the same directory - Probably in your case...
    // Just comment out the 2 lines above this and uncomment the line
    // that follows.
    //BufferedReader buffer = new BufferedReader(new FileReader(filename));

    String line;/* w w w .  j a  v  a 2  s .  c o  m*/
    int row = 0;

    while ((line = buffer.readLine()) != null) {
        String[] vals = line.trim().split("\\s+");

        // Lazy instantiation.
        if (matrix == null) {
            size = vals.length;
            matrix = new int[size][size];
            log10 = (int) Math.floor(Math.log10(size * size)) + 1;
            numberFormat = String.format("%%%dd", log10);
        }

        for (int col = 0; col < size; col++) {
            matrix[row][col] = Integer.parseInt(vals[col]);
        }

        row++;
    }
}

From source file:org.candlepin.client.cmds.Utils.java

public static Properties getDefaultProperties() {
    try {// ww  w.j  a  v  a  2  s  . co m
        Properties properties = new Properties();
        properties.load(ClassLoader.getSystemResourceAsStream(DEF_PROPERTIES_PATH));
        replaceSystemPropertyValues(properties, Constants.CP_HOME_DIR);
        return properties;
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}