??????davenkin???????ResourceLoader.java??????????????

package davenkin;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class ResourceLoader
{
    public static void main(String[] args) throws IOException
    {
        ResourceLoader resourceLoader = new ResourceLoader();
        resourceLoader.loadProperties1();

    }

    public void loadProperties1() throws IOException
    {
        InputStream input = null;
        try
        {
            input = Class.forName("davenkin.ResourceLoader").getResourceAsStream("/resources/config.properties");

            //also can be this way:
            //input = this.getClass().getResourceAsStream("/resources/config.properties");
        } catch (ClassNotFoundException e)
        {
            e.printStackTrace();
        }
        printProperties(input);
    }

    private void printProperties(InputStream input) throws IOException
    {
        Properties properties = new Properties();
        properties.load(input);
        System.out.println(properties.getProperty("name"));
    }
}

????????????????resources???????config.properties???????

ConfigUnderSrc

?????????????????ReourceLoader.java????е?????????????????????????·????/resources/config.properties????"/"??????????????λ?????????????????????classpath?μ?resources????С????????????????·?????"/"??????????????λ??????????????davenkin/resources/config.properties??????????

????????????ClassLoader???????????

????ClassLoader???????Class???????????????

????public InputStream getResourceAsStream(String pathToConfigFile)??

??????ClassLoader??????????????pathToConfigFile????????"/"????????????????classpath????в????Class??????????????????????(delegate)??ClassLoader?????????????ο?Java????????