Example usage for java.lang Class getResourceAsStream

List of usage examples for java.lang Class getResourceAsStream

Introduction

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

Prototype

@CallerSensitive
public InputStream getResourceAsStream(String name) 

Source Link

Document

Finds a resource with a given name.

Usage

From source file:MainClass.java

public static void main(String args[]) {
    SynthLookAndFeel synth = new SynthLookAndFeel();
    try {/* w w w  .  ja  v  a 2 s . com*/
        Class aClass = MainClass.class;
        InputStream is = aClass.getResourceAsStream("config.xml");
        if (is == null) {
            System.err.println("Unable to find theme configuration");
            System.exit(-1);
        }
        synth.load(is, aClass);
    } catch (ParseException e) {
        System.err.println("Unable to load theme configuration");
        System.exit(-2);
    }
    try {
        UIManager.setLookAndFeel(synth);
    } catch (javax.swing.UnsupportedLookAndFeelException e) {
        System.err.println("Unable to change look and feel");
        System.exit(-3);
    }
    JFrame frame = new JFrame("Synth Sample");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JButton button = new JButton("Hello, Synth");
    frame.add(button, BorderLayout.CENTER);
    JTextField textField = new JTextField();
    frame.add(textField, BorderLayout.SOUTH);
    frame.setSize(300, 200);
    frame.setVisible(true);
}

From source file:name.livitski.databag.cli.Launcher.java

public static void main(String[] args) {
    Launcher tool = new Launcher();
    LogManager logging = LogManager.getLogManager();
    Class<?> beanClass = tool.getClass();
    InputStream cfg = beanClass.getResourceAsStream("/logging.properties");
    try {//ww w  .  j  a  v  a  2  s .  c o m
        logging.readConfiguration(cfg);
    } catch (Exception e) {
        System.err.println("WARNING: could not initialize logging. Detailed diagnostics may not be available. "
                + e.getMessage());
    } finally {
        try {
            cfg.close();
            cfg = null;
        } catch (Exception ignored) {
        }
    }
    try {
        tool.setOptions(SYNTAX.parseCommandLine(args));
    } catch (ParseException e) {
        tool.log().severe(e.getMessage());
        showHelpHint();
        System.exit(Status.SYNTAX.getCode());
    }
    if (null != tool.getOptions())
        tool.run();
    if (Status.OK != tool.getStatus())
        System.exit(tool.getStatus().getCode());
}

From source file:Main.java

/**
 * Returns an InputStream for the given test class and sub-path.
 *
 * @param testClass A Junit test class./*  ww  w .  j  av  a  2  s. c  o  m*/
 * @param subPath   The sub-path under androidTest/resources where the desired resource is
 *                  located. Should not be prefixed with a '/'
 */
public static InputStream openResource(Class testClass, String subPath) {
    return testClass.getResourceAsStream("/" + subPath);
}

From source file:Main.java

public static InputStream getWsddStream(Class<?> clientClass) {
    InputStream wsdd = clientClass.getResourceAsStream("client-config.wsdd");
    if (wsdd == null) {
        throw new IllegalStateException(
                "Could not find client-config.wsdd relative to " + clientClass.getName());
    }/*from   w w  w  .jav  a 2s .  c  o m*/
    return wsdd;
}

From source file:Main.java

public static Image getImage(Class relativeClass, String filename) {
    Image returnValue = null;//w w w  .  j  a v  a  2 s. c  o  m
    InputStream is = relativeClass.getResourceAsStream(filename);
    if (is != null) {
        BufferedInputStream bis = new BufferedInputStream(is);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        try {
            int ch;
            while ((ch = bis.read()) != -1) {
                baos.write(ch);
            }
            Toolkit t = Toolkit.getDefaultToolkit();
            returnValue = t.createImage(baos.toByteArray());
        } catch (IOException exception) {
            System.err.println("Error loading: " + filename);
        }
    }
    return returnValue;
}

From source file:com.boozallen.cognition.test.utils.TestResourceUtils.java

public static InputStream getResourceAsStream(Class<?> testClass, String resource) {
    return testClass.getResourceAsStream(getResourcePath(testClass, resource));
}

From source file:de.fraunhofer.iais.eis.jrdfb.util.FileUtils.java

public static String readResource(String fileName, Class<?> resourceClass) throws IOException {

    return IOUtils.toString(resourceClass.getResourceAsStream(fileName), "UTF-8");
}

From source file:Main.java

private static ImageInputStream getResourceAsStream(Class<?> aClass, String name) throws IOException {
    final InputStream is = aClass.getResourceAsStream(name);

    if (is == null) {
        throw new IOException(MessageFormat.format("resource {0} not found", name));
    }/*  www  . j a v  a 2s.  com*/
    return new FileCacheImageInputStream(is, null);
}

From source file:com.switchfly.compress.TestingUtil.java

public static String readFile(Class clazz, String resource) throws IOException {
    return IOUtils.toString(clazz.getResourceAsStream(resource));
}

From source file:org.talend.components.salesforce.TestUtils.java

public static String getResourceAsString(Class clazz, String path, String encoding) throws IOException {
    InputStream is = clazz.getResourceAsStream(path);
    if (is == null) {
        throw new FileNotFoundException(String.format("Resource not found: %s, %s", clazz.getName(), path));
    }/* w ww .  ja  v  a2  s  . com*/
    return IOUtils.toString(is, encoding);
}