java读取properties文件的两种方式

时间:2022-02-13 15:35:43

配置文件在src目录下

直接上代码了

package com.po.test;

import java.io.IOException;
import java.util.Properties;
import java.util.ResourceBundle;

public class Test {
public static void main(String[] args) {
Properties prop = new Properties();
try {
prop.load(ClassLoader.getSystemResourceAsStream("test.properties"));
String url = (String) prop.get("url");
System.out.println("Properties方法获取:"+url);
} catch (IOException e) {
e.printStackTrace();
}
ResourceBundle bundle = ResourceBundle.getBundle("test");
String url = bundle.getString("url");
System.out.println("ResourceBundle方法获取:"+url);
}
}