Java获取Properties文件中值的方法
来源:互联网 发布:三国之数据辅助103 编辑:程序博客网 时间:2024/05/02 08:58
在遇到钉钉开发的时候需要将任务调度每次获取到的access_token赋给一个常量类中的静态属性,
需要获取到配置文件中的properties文件的值
- import java.io.BufferedInputStream;
- import java.io.FileInputStream;
- import java.io.IOException;
- import java.io.InputStream;
- import java.util.Enumeration;
- import java.util.Properties;
- import java.util.PropertyResourceBundle;
- import java.util.ResourceBundle;
- public class Test {
- // 1。使用java.util.Properties 类的load()方法
- // 示例:
- public static String getProperties_1(String url) throws IOException {
- InputStream in = new BufferedInputStream(new FileInputStream(url));
- Properties p = new Properties();
- p.load(in);
- return p.getProperty("jdbc.type");
- }
- // 2。使用java.util.ResourceBundle类的getBundle()方法
- // 示例:
- public static void getProperties_2(String url) {
- ResourceBundle rb = ResourceBundle.getBundle(url);
- Enumeration<String> keys = rb.getKeys();
- while (keys.hasMoreElements()){
- System.out.println(rb.getString(keys.nextElement()));
- }
- }
- // 3。使用java.util.PropertyResourceBundle类的构造函数
- // 示例:
- public static void getProperties_3(String url) throws IOException {
- InputStream in = new BufferedInputStream(new FileInputStream(url));
- ResourceBundle rb = new PropertyResourceBundle(in);
- Enumeration<String> keys = rb.getKeys();
- while (keys.hasMoreElements()){
- System.out.println(rb.getString(keys.nextElement()));
- }
- }
- // 4。使用class变量的getResourceAsStream()方法
- // 示例:
- public static String getProperties_4(String url) throws IOException {
- InputStream in = Test.class.getResourceAsStream(url);
- Properties p = new Properties();
- p.load(in);
- return p.getProperty("jdbc.url");
- }
- // 5。使用class.getClassLoader()所得到的java.lang.ClassLoader的getResourceAsStream()方法
- // 示例:
- public static String getProperties_5(String url) throws IOException {
- InputStream in = Test.class.getClassLoader().getResourceAsStream(url);
- Properties p = new Properties();
- p.load(in);
- return p.getProperty("jdbc.url");
- }
- // 6。使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
- // 示例:
- public static String getProperties_6(String url) throws IOException {
- InputStream in = ClassLoader.getSystemResourceAsStream(url);
- Properties p = new Properties();
- p.load(in);
- return p.getProperty("jdbc.url");
- }
- //
- // 补充
- // Servlet中可以使用javax.servlet.ServletContext的getResourceAsStream()方法
- // 示例:
- // public static Properties getProperties_8(String url) {
- // InputStream in = context.getResourceAsStream(url);
- // Properties p = new Properties ();
- // p.load(in);
- // }
- public static void main(String[] args) throws IOException {
- //需要将文件放置到项目根目录下面
- System.out.println(getProperties_1("settings.properties"));
- System.out.println("----------");
- //需要将文件放置到class文件同级目录下面
- getProperties_2("settings");
- System.out.println("----------");
- getProperties_3("settings.properties");
- System.out.println("----------");
- System.out.println(getProperties_4("settings.properties"));
- System.out.println("----------");
- System.out.println(getProperties_5("settings.properties"));
- System.out.println("----------");
- System.out.println(getProperties_6("settings.properties"));
- }
- }
spring xml文件需要的配置
java代码:- package cn.yufu.system.modules.oa.merchant.web;
- import cn.yufu.system.common.web.BaseController;
- import cn.yufu.system.modules.oa.merchant.service.ActBusApprovalService;
- @Controller
- @RequestMapping(value = "${adminPath}/merchant/actBusApproval")
- public class ActBusApprovalController extends BaseController {
- @Autowired
- private ActBusApprovalService actBusApprovalService;
- // APP_PROP 与配置文件的id相同
- @Value("#{APP_PROP['fileUploadPath']}")
- private String uploadFilePath;
- }
阅读全文
0 0
- Java获取Properties文件中值的方法
- Java获取Properties文件中值的方法
- Java获取Properties文件中值的方法
- Java获取Properties文件中值的方法
- java获取properties文件值得方法
- Java获取.properties文件
- Java获取properties文件
- java获取properties文件
- 使用spring注解方法读取properties文件中值
- 使用spring注解方法读取properties文件中值
- Java中Properties文件值的获取
- java 读取 properties文件的各种方法
- Java读取.properties文件的方法
- java读取Properties文件的方法
- java读取Properties属性文件的方法
- Java读取properties文件的简单方法
- java读取.properties文件的方法总结
- java 读取properties文件的方法
- scrapy安装win32api,找不到包解决方法
- 4Sum:带重复数组取值求和
- [Python]
- PHP与JS跨域请求
- CentOS6.5中安装JDK
- Java获取Properties文件中值的方法
- Java反射实战
- 国家公务员考试之证监会计算机类
- 创投日报:12月13日收录投融资项目23起
- 10G多通道带内FEC波分复用传输设备解决方案
- 从“平板之父”到连续创业者, Jerry Kaplan传奇来袭
- 阿里与俄最大银行谈判破裂;今日头条就咏宁事件起诉新浪;谷歌成立AI中国中心丨价值早报
- 最后一批90后开始养生了,中医科普短视频会火吗?
- Rxjava+retrofit 添加公共请求参数及请求结果封装