Load a properties fileTag(s): Language

import java.util.Properties;
import java.io.File;
import java.io.IOException;
import java.io.FileInputStream;
import java.net.URL;
public class PropsUtils {
    private PropsUtils() {  }
    /**
     * Load a properties file from the classpath
     * @param propsName
     * @return Properties
     * @throws Exception
     */
    public static Properties load(String propsName) throws Exception {
        Properties props = new Properties();
        URL url = ClassLoader.getSystemResource(propsName);
        props.load(url.openStream());
        return props;
    }
    /**
     * Load a Properties File
     * @param propsFile
     * @return Properties
     * @throws IOException
     */
    public static Properties load(File propsFile) throws IOException {
        Properties props = new Properties();
        FileInputStream fis = new FileInputStream(propsFile);
        props.load(fis);
        fis.close();
        return props;
    }
}

Load from the startup directory of your application (ex. directory containing the jar)

java.util.Properties props = new java.util.Properties();
String path = getClass().getProtectionDomain().getCodeSource().
   getLocation().toString().substring(6);
java.io.FileInputStream fis = new java.io.FileInputStream
   (new java.io.File( path + "/myprops.props"));
props.load(fis);
fis.close();
System.out.println(props);
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。