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文件:
username=mamama
nickname=xiaoma
password=123456
输出结果:

java加载properties文件的六中基本方式实现的更多相关文章
- java加载properties文件的六种方法总结
java加载properties文件的六种方法总结 java加载properties文件的六中基本方式实现 java加载properties文件的方式主要分为两大类:一种是通过import java. ...
- JAVA加载Properties配置资源文件
JAVA加载Properties配置资源文件 制作人:全心全意 配置文件(资源文件):以properties作为拓展名的文件 Java代码是如何加载properties文件的? 必须使用Propert ...
- Java开发学习(八)----IOC/DI配置管理第三方bean、加载properties文件
前面的博客都是基于我们自己写的类,现在如果有需求让我们去管理第三方jar包中的类,该如何管理? 一.案例:数据源对象管理 本次案例将使用数据源Druid和C3P0来配置学习下. 1.1 环境准备 学习 ...
- spring入门(二)【加载properties文件】
在开发过程当中需要用到配置信息,这些信息不能进行硬编码,这时配置文件是一个比较好的方式,java提供了properties格式的文件,以键值对的方式保存信息,在读取的时候通过键获得键对应的值,spri ...
- Java加载资源文件的两种方法
处理配置文件对于Java程序员来说再常见不过了,不管是Servlet,Spring,抑或是Structs,都需要与配置文件打交道.Java将配置文件当作一种资源(resource)来处理,并且提供了两 ...
- Spring加载properties文件的两种方式
在项目中如果有些参数经常需要修改,或者后期可能需要修改,那我们最好把这些参数放到properties文件中,源代码中读取properties里面的配置,这样后期只需要改动properties文件即可, ...
- JavaEE互联网轻量级框架整合开发(书籍)阅读笔记(12):XML配置自动扫描包,自动加载*.properties文件
一.XML和注解组合使用 前几篇的测试案例都是在Java类中配置,现在换一种使用方式,在XML中配置,使Spring IoC容器在启动之后自动去扫描配置的包路径,扫描加载指定路径下的propertie ...
- xml文件 加载properties文件的两种方法与注意事项
1.遇到的问题: 配置redisSpringContext.xml 时,遇到 properties加载失败,提示BeanDefinitionStoreException 和 java.lang. ...
- Spring加载properties文件的属性的值
要使用配置文件的值首先在spring.xml配置加载properties文件 <context:property-placeholder location="classpath:ife ...
随机推荐
- SQL优化--inner、left join替换in、not in、except
新系统上线,用户基数16万,各种查询timeout.打开砂锅问到底,直接看sql语句吧,都是泪呀,一大堆in\not in\except.这里总结一下,怎么替换掉in\not in\except. 1 ...
- 接触新的项目,构建时候报错:Failure to find io.netty:netty-tcnative:jar:${os.detected.classifier}:2.0.7.Final in
详细信息如下: Failure to find io.netty:netty-tcnative:jar:${os.detected.classifier}:2.0.7.Final in http:// ...
- 阿里云SLB出现502 Bad Gateway 错误排查解决方法
502 Bad Gateway The proxy server received an invalid response from an upstream server. 原本系统是通过一个SLB转 ...
- AJAX快速上手和基本核心
一.快速上手AJAX 使用ajax的过程可以类比平常我们访问网页过程 1.创建一个XMLHttpRequest类型的对象------相当于打开了浏览器 var xhr = new XMLHttpReq ...
- jquery删除内容是动态修改序号
如图,点击删除图标的时候要删除当前的一条记录,同时界面上的序号要动态的排列好 以下是html结构: jquery实现思路: 首先,需要获取到当前要删除盒子的序号$indexCur,然后遍历父盒子,取出 ...
- .NET Core Session的简单使用
前言 在之前的.NET 里,我们可以很容易的使用Session读取值.那今天我们来看看 如何在.NET Core中读取Session值呢? Session 使用Session之前,我们需要到Start ...
- 如何开发AR增强现实应用与产品
2016年被称为VR元年,可见火爆程度,但是我要告诉你,其实还有一种技术AR(增强现实)技术,才是下一个真正的“风口”技术.可以预见的是,未来AR应用爆发之时,必将超越VR产业规模,开拓千亿级市场空间 ...
- word中如何只修改英文的颜色
替换->更多->使用通配符,查找[a-zA-Z],替换为^&,字体选红色
- 微信公众号开发C#系列-5、用户和用户组管理-支持同步
1.概述 眼前时下流行的经济有个叫粉丝经济,粉丝带动收益.一个好运营良好的公众号肯定会有一大批的粉丝团,如何挖掘粉丝来产生效益,是微信营销的关键.微信公众号后台本身提供了粉丝(用户)与用户分组的管理, ...
- kubernetes系列08—service资源详解
本文收录在容器技术学习系列文章总目录 1.认识service 1.1 为什么要使用service Kubernetes Pod 是有生命周期的,它们可以被创建,也可以被销毁,然而一旦被销毁生命就永远结 ...