• Load a properties file Real's Java Howto


    Load a properties file - Real's Java How-to

    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);
  • 相关阅读:
    仿微博的JQuery日历控件
    JavaScript演示下Singleton设计模式
    简短的创建Ajax对象代码
    C++常用速查
    学习网站
    Ubuntu纯命令行安装并配置Teamviewer
    在Jupyter中使用自定义conda环境
    TensorFLow: Gradient Clipping
    Python partial 工具函数
    lambda X, y: X
  • 原文地址:https://www.cnblogs.com/lexus/p/2480300.html
Copyright © 2020-2023  润新知