首页 > 编程语言 > 详细

Java程序读取Properties文件

时间:2017-11-04 14:25:22      阅读:241      评论:0      收藏:0      [点我收藏+]

1.读取文件代码如下:

技术分享
/**
* 配置文件为settings.properties
* YourClassName对应你类的名字
*
/ 
private Properties LoadSettings() {
        Properties prop = new Properties();
        
        try {
            InputStream  in = new BufferedInputStream(YourClassName.class.getClassLoader().getResourceAsStream("/settings.properties"));
            prop.load(in); 
            in.close();
        } catch (Exception e) {
            System.err.println("Load settings.properties file Error! :\r\n"+ e.getMessage());
            prop = null;
        }
        
        return prop;
    }
View Code

2.使用上述代码来读取配置文件需要注意的是,配置文件必须放在src目录下,而不能放在子目录下:

    技术分享

3. 特别注意,配置文件中的内容不需要加双引号;

#不需要加双引号,加了之后获取到的内容可能就不是你想要的结果
smtp_host="smtp-relay.gmail.com"
smtp_user="debug@abc.com"
smtp_password="debug"
send_to="support@abc.com"
smtp_port="465"


#这样是对的
smtp_host=smtp-relay.gmail.com
smtp_user=debug@abc.com
smtp_password=debug
send_to=support@abc.com
smtp_port=465

 

Java程序读取Properties文件

原文:http://www.cnblogs.com/tommy-huang/p/7783000.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!