欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

加载Properties文件

程序员文章站 2022-03-05 09:25:59
...
// Load the user settings.
String userSettingsFile = System.getProperty("user.home")+"/user_settings.ini";
BufferedReader in = null;
try {
    in = Utilities.getBufferedReader(userSettingsFile);
    settings.load(in);
    in.close();
} catch (Exception e) {
    /* Program starts with default settings. */
}
finally {
    if(in != null)
    try {
        in.close();
    } catch (IOException e) {}
}

 

/**
 * Returns a <code>BufferedReader</code> to the file corresponding to the passed filename.
 * <p>
 * This method searches for the file in the class path.
 *  
 * @param filename  the name of the file
 * @return the <code>BufferedReader</code> to the file corresponding to the passed filename
 */
public static BufferedReader getBufferedReader(String filename) {

    BufferedReader b = null;

    // Try to read the file, no matter if it is in the class path or not.
    try {
        b = new BufferedReader(new FileReader(filename));	
    } catch (Exception e) {}

    // If the program is started as web start application the file 
    // may be in the jar file.Hence try to read it from the jar, too.
    try {
        if(b == null)
	    b = new BufferedReader(new InputStreamReader(Main.class.getResourceAsStream(filename)));			
    } catch (Exception e) {}
        return b;
}
 
相关标签: Web