Java ClassLoader Load getReader(final String name, final String encoding)

Here you can find the source of getReader(final String name, final String encoding)

Description

get Reader

License

Apache License

Declaration

public static Reader getReader(final String name, final String encoding) throws IOException 

Method Source Code

//package com.java2s;
/*/*from   w w w.  java2 s . c  o m*/
 * Copyright 2016 Christoph B?hme
 *
 * Licensed under the Apache License, Version 2.0 the "License";
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import java.net.URL;

public class Main {
    public static Reader getReader(final String name) throws FileNotFoundException {
        return new InputStreamReader(getStream(name));
    }

    public static Reader getReader(final File file) throws FileNotFoundException {
        return new InputStreamReader(getStream(file));
    }

    public static Reader getReader(final String name, final String encoding) throws IOException {
        return new InputStreamReader(getStream(name), encoding);
    }

    public static Reader getReader(final File file, final String encoding) throws IOException {
        return new InputStreamReader(getStream(file), encoding);
    }

    /**
     * First attempts to open open {@code name} as a file. On fail attempts to
     * open resource with name {@code name}. On fail attempts to open {@code name}
     * as a URL.
     *
     * @param name name of the file or resource to open
     * @return an input stream for reading the opened file or resource
     * @throws FileNotFoundException if all attempts fail
     */
    public static InputStream getStream(final String name) throws FileNotFoundException {
        if (name == null) {
            throw new IllegalArgumentException("'name' must not be null");
        }
        final File file = new File(name);
        if (file.exists()) {
            return getStream(file);
        }

        InputStream stream = Thread.currentThread().getContextClassLoader().getResourceAsStream(name);
        if (stream != null) {
            return stream;
        }

        try {
            stream = new URL(name).openStream();
        } catch (final IOException e) {
            throwFileNotFoundException(name, e);
        }
        if (stream == null) {
            throwFileNotFoundException(name, null);
        }

        return stream;

    }

    public static InputStream getStream(final File file) throws FileNotFoundException {
        return new FileInputStream(file);
    }

    private static void throwFileNotFoundException(final String name, final Throwable t)
            throws FileNotFoundException {
        final FileNotFoundException e = new FileNotFoundException("No file, resource or URL found: " + name);
        if (t != null) {
            e.initCause(t);
        }
        throw e;
    }
}

Related

  1. getPath(final String fileName)
  2. getPath(String resoureLocation)
  3. getProhibitedProxyInterfaces()
  4. getProperties(String bundleName, String fileName)
  5. getProperty(String propertyName)
  6. getRootPath()
  7. getSimpleName(String fqcn)
  8. getSourcesPath()
  9. getSSTable(String version, int generation)