前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java读取properties配置

Java读取properties配置

作者头像
suveng
发布2019-09-18 14:16:17
2.1K0
发布2019-09-18 14:16:17
举报

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。

本文链接:https://blog.csdn.net/qq_37933685/article/details/81747749

个人博客:https://suveng.github.io/blog/​​​​​​​

Java读取properties配置

实际开发中,我们一般情况下,会把配置相关的信息,放在 xx.properties 中保存,并且使用。因为,以后代码有可能写得非常复杂,像一些不经常改动的东西,都建议抽取出来保存到配置文件中。当我们要用的时候,直接从配置文件中获取使用。当我们需要修改的时候,显示找到文件,然后就可以直接修改即可

码云代码地址

https://gitee.com/suwenguang/SpringFrameworkDemo

注意这个工程还有我其他demo,本文目录在spring.my.suveng.Java读取properties 目录里面

  1. 新建 test.properties 配置文件 driver=com.mysql.jdbc.Driver url=jdbc:mysql:///hello user=root password=1234
  2. 编写工具类 /** * author Veng Su * email suveng@163.com * date 2018/8/16 19:30 */ public class ReadProperties { public void loadProperties() throws IOException { // 1.找到文件,加载文件 // 1.使用java.util.Properties类的load()方法: InputStream is = new BufferedInputStream(new FileInputStream("src/main/java/spring/my/suveng/Java读取properties/properties/test.properties")); Properties properties = new Properties(); properties.load(is); // 2. 通过输入流加载数据 getData(properties); } private void getData(Properties properties) { List<String> list = new ArrayList<String>(); list.add(properties.getProperty("driver")); list.add(properties.getProperty("url")); list.add(properties.getProperty("user")); list.add(properties.getProperty("password")); System.out.println(list); } @Test public void test() throws IOException { ReadProperties readProperties=new ReadProperties(); readProperties.loadProperties(); } }

测试

代码语言:javascript
复制
@Test
public void test() throws IOException {
    ReadProperties readProperties=new ReadProperties();
    readProperties.loadProperties();
}

运行结果:

1534420395663
1534420395663

加油

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018年08月16日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Java读取properties配置
    • 码云代码地址
      • 测试
        • 运行结果:
        领券
        问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档