Read file as a Input Stream from given resource - Java java.io

Java examples for java.io:InputStream Read

Description

Read file as a Input Stream from given resource

Demo Code

/*// w ww. j  ava2  s .c  o  m
 * Open Source Business Intelligence Tools - http://www.osbitools.com/
 * 
 * Copyright 2014-2016 IvaLab Inc. and by respective contributors (see below).
 * 
 * Released under the LGPL v3 or higher
 * See http://www.gnu.org/licenses/lgpl-3.0.html
 *
 * Date: 2015-03-15
 * 
 * Contributors:
 * 
 * Igor Peonte <igor.144@gmail.com>
 * 
 */
import java.io.*;
import java.net.*;
import java.util.*;
import java.util.jar.*;
import java.nio.charset.StandardCharsets;
import com.osbitools.ws.shared.GenericUtils;

public class Main{
    /**
     * Read file as a Input Stream from given resource
     * @param path Resource Path
     * @return File Input Stream
     * @throws IOException
     */
    public static InputStream readJarFileAsStream(String path)
            throws IOException {
        InputStream in = ClassLoader.getSystemClassLoader()
                .getResourceAsStream(path);

        if (in == null)
            throw new IOException("Empty Resource [" + path + "]");

        return in;
    }
}

Related Tutorials