Java ClassPath getInputStreamFromClasspathFile(final String fileName)

Here you can find the source of getInputStreamFromClasspathFile(final String fileName)

Description

get Input Stream From Classpath File

License

Apache License

Declaration

public static InputStream getInputStreamFromClasspathFile(final String fileName) throws FileNotFoundException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.URL;

public class Main {
    public static InputStream getInputStreamFromClasspathFile(final String fileName) throws FileNotFoundException {
        return new FileInputStream(getFileFromClasspath(fileName));
    }//from ww  w  .  j  av a 2  s  . c  om

    /**
     * Load the resource either using current class loader or using parent class
     * loader
     * 
     * @param fileName
     * @return
     */
    public static File getFileFromClasspath(final String fileName) {
        File file = null;

        // Load the resource using current class loader
        URL url = Thread.currentThread().getClass().getResource(fileName);

        if (url != null) {
            file = new File(url.getFile());
        } else {
            // load the resource using the parent context
            url = Thread.currentThread().getContextClassLoader().getResource(fileName);
            if (url != null) {
                file = new File(url.getFile());
            }
        }

        return file;
    }
}

Related

  1. getFileFromClasspath(String fileName)
  2. getFileFromClasspath(String name)
  3. getFileFromClasspathResource(String resource)
  4. getFilePathFromClasspath(String filename)
  5. getFullPath(String classPath)
  6. getResourceFromClasspath(final String fileName)
  7. getStreamFromClassPath(String source)
  8. getSystemClassPath()
  9. getSystemClasspathEntries()