Here you can find the source of getResource(final String resource)
Parameter | Description |
---|---|
resource | The resource to be read. |
Parameter | Description |
---|---|
IllegalArgumentException | When the configuration file could not befound or another I/O error occurs. |
public static Properties getResource(final String resource)
//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; } }