java加载properties文件的六种方法总结
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
java加载properties⽂件的六种⽅法总结
java加载properties⽂件的六种⽅法总结
java加载properties⽂件的六中基本⽅式实现
java加载properties⽂件的⽅式主要分为两⼤类:⼀种是通过import java.util.Properties类中的load(InputStream in)⽅法加载;另⼀种是通过import java.util.ResourceBundle类的getBundle(String baseName)⽅法加载。
注意:⼀定要区分路径格式
实现代码如下:
package com.util;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
public class PropertiesUtil {
private static String basePath = "src/prop.properties";
private static String name = "";
private static String nickname = "";
private static String password = "";
/**
* ⼀、使⽤java.util.Properties类的load(InputStream in)⽅法加载properties⽂件
*
*/
public static String getName1() {
try {
Properties prop = new Properties();
InputStream is = new FileInputStream(basePath);
prop.load(is);
name = prop.getProperty("username");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return name;
}
/**
* ⼆、使⽤class变量的getResourceAsStream()⽅法
* 注意:getResourceAsStream()读取路径是与本类的同⼀包下
*
*/
public static String getName2() {
Properties prop = new Properties();
InputStream is = PropertiesUtil.class
.getResourceAsStream("/com/util/prop.properties");
try {
prop.load(is);
name = prop.getProperty("username");
} catch (IOException e) {
e.printStackTrace();
}
return name;
}
/**
* 三、
* 使⽤class.getClassLoader()所得到的ng.ClassLoader的getResourceAsStream()⽅法
* getResourceAsStream(name)⽅法的参数必须是包路径+⽂件名+.后缀否则会报空指针异常
*
*/
public static String getName3() {
Properties prop = new Properties();
InputStream is = PropertiesUtil.class.getClassLoader()
.getResourceAsStream("com/util/prop.properties");
try {
prop.load(is);
} catch (IOException e) {
e.printStackTrace();
}
return name;
}
/**
* 四、使⽤ng.ClassLoader类的getSystemResourceAsStream()静态⽅法
* getSystemResourceAsStream()⽅法的参数格式也是有固定要求的
*
*/
public static String getName4() {
Properties prop = new Properties();
InputStream is = ClassLoader
.getSystemResourceAsStream("com/util/prop.properties");
try {
prop.load(is);
name = prop.getProperty("username");
} catch (IOException e) {
e.printStackTrace();
}
return name;
}
/**
* 五、使⽤java.util.ResourceBundle类的getBundle()⽅法
* 注意:这个getBundle()⽅法的参数只能写成包路径+properties⽂件名,否则将抛异常 *
*/
public static String getName5() {
ResourceBundle rb = ResourceBundle.getBundle("com/util/prop");
password = rb.getString("password");
return password;
}
/**
* 六、使⽤java.util.PropertyResourceBundle类的构造函数
*
*/
public static String getName6() {
try {
InputStream is = new FileInputStream(basePath);
ResourceBundle rb = new PropertyResourceBundle(is);
nickname = rb.getString("nickname");
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return nickname;
}
/**
* 测试
*
*/
public static void main(String[] args) {
System.out.println("name1:" + PropertiesUtil.getName1());
System.out.println("name2:" + PropertiesUtil.getName2());
System.out.println("name3:" + PropertiesUtil.getName3());
System.out.println("name4:" + PropertiesUtil.getName4());
System.out.println("password:" + PropertiesUtil.getName5());
System.out.println("nickname:" + PropertiesUtil.getName6());
}
}
⽂件路径:
prop.properties⽂件: