Java Resource Load getResource(final String resource)

Here you can find the source of getResource(final String resource)

Description

Reads configuration from a classpath resource stream obtained from the current thread's class loader through ClassLoader#getSystemResourceAsStream(String) .

License

Apache License

Parameter

Parameter Description
resource The resource to be read.

Exception

Parameter Description
IllegalArgumentException When the configuration file could not befound or another I/O error occurs.

Return

A object read from the specified resource.

Declaration

public static Properties getResource(final String resource) 

Method Source Code

//package com.java2s;
/**//from   ww  w  . ja  v a 2  s.  co m
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.IOException;
import java.io.InputStream;

import java.util.Properties;

public class Main {
    /**
     * Reads configuration from a classpath resource stream obtained from the
     * current thread's class loader through
     * {@link ClassLoader#getSystemResourceAsStream(String)}.
     *
     * @param resource The resource to be read.
     * @return A {@link Properties} object read from the specified
     * resource.
     * @throws IllegalArgumentException When the configuration file could not be
     *                                  found or another I/O error occurs.
     */
    public static Properties getResource(final String resource) {
        InputStream input = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
        if (input == null) {
            throw new IllegalArgumentException("configuration file '" + resource + "' not found on classpath");
        }

        final Properties config = new Properties();
        try {
            config.load(input);
        } catch (final IOException e) {
            throw new IllegalArgumentException("reading configuration from '" + resource + "' failed", e);
        }
        return config;
    }
}

Related

  1. getResource(Class clazz, String fileName)
  2. getResource(Class clazz, String name, String charset)
  3. getResource(Class c, String name)
  4. getResource(ClassLoader classLoader, final String path)
  5. getResource(final String aResName)
  6. getResource(String fileName)
  7. getResource(String fileName, int type)
  8. getResource(String name)
  9. getResource(String path)