java加载properties文件的六种方法总结
作者:lqh 时间:2023-09-20 05:24:54
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()所得到的java.lang.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;
}
/**
* 四、 使用java.lang.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文件:
1 username=mamama
2 nickname=xiaoma
3 password=123456
输出结果:
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
来源:http://www.cnblogs.com/heizai002/p/6859121.html
标签:java,properties
0
投稿
猜你喜欢
Android新浪微博下拉刷新(最新消息显示在最上面)
2023-06-04 00:56:49
C#图表算法之有向图
2021-11-25 14:50:57
C# SynchronizationContext以及Send和Post使用解读
2023-10-16 04:27:28
Mybatis-Plus进阶分页与乐观锁插件及通用枚举和多数据源详解
2023-11-23 11:00:58
SpringBoot Web详解静态资源规则与定制化处理
2022-01-23 16:19:57
Android在JNI中使用ByteBuffer的方法
2021-11-08 21:14:35
详解C#如何实现分割视频
2022-12-07 10:48:23
安卓逆向分析之酷狗signature案例分享
2022-12-06 21:23:37
详解关于Windows10 Java环境变量配置问题的解决办法
2023-02-06 10:53:29
JAVA实现PDF转HTML文档的示例代码
2021-10-18 12:47:00
c++指针使用形参改变实参的方法
2023-03-11 22:40:21
Java ArrayList扩容问题实例详解
2022-05-08 08:57:57
Android游戏开发实践之人物移动地图的平滑滚动处理
2023-12-26 19:45:41
spring boot如何加入mail邮件支持
2021-09-27 15:12:57
Android判断当前App是在前台还是在后台
2022-09-20 10:37:49
java按钮控件数组实现计算器界面示例分享
2021-09-12 22:37:59
C#中事务处理和非事务处理方法实例分析
2023-12-23 08:09:13
Java HashMap的工作原理
2022-12-25 14:39:02
Android调用系统摄像头拍照并显示在ImageView上
2022-02-19 17:42:00
Java 超详细讲解核心类Spring JdbcTemplate
2021-08-05 15:30:57