首页 > 编程语言 > 详细

java加载properties文件的方法记录

时间:2021-04-25 00:20:28      阅读:28      评论:0      收藏:0      [点我收藏+]
技术分享图片

 1 package com.example.tempshiro.util;
 2 
 3 import java.io.InputStream;
 4 import java.util.Properties;
 5 
 6 public class LoadPropertiesUtil {
 7 
 8     public static Properties loadProperties(String name) {
 9         Properties prop = null;
10         try {
11             prop = new Properties();
12             //不同加载方式要求的文件路径不同
13             //1.
14 //            InputStream is = new FileInputStream("src/main/resources/test.properties");
15             //2.
16 //            InputStream is = Thread.currentThread().getContextClassLoader()
17 //                    .getResourceAsStream("test.properties");
18             //3.
19 //            InputStream is = Properties.class
20 //                    .getResourceAsStream("/test.properties");
21             //4.
22             InputStream is = ClassLoader
23                     .getSystemResourceAsStream("test.properties");
24             prop.load(is);
25         } catch (Exception e) {
26             e.printStackTrace();
27         }
28         return prop;
29     }
30 
31     public static void main(String[] args) {
32         Properties properties = loadProperties("test.properties");
33         System.out.println(properties.get("name"));
34     }
35 }

 



 

java加载properties文件的方法记录

原文:https://www.cnblogs.com/mistyTime-2016/p/14698347.html

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