Properties类调用setProperty方法将键值对保存到内存中,此时可以通过getProperty方法读取,propertyNames方法进行遍历,但是并没有将键值对持久化到属性文件中,故需要调用store方法持久化键值对到属性文件中。
public static void main(String[] args) throws IOException {
Properties properties = new Properties();
OutputStream output = null;
try {
output = new FileOutputStream(“src/main/resources/config.properties”);
properties.setProperty(“username”, “root”);
properties.setProperty(“password”, “123456”);
// 保存键值对到文件中
properties.store(output, “JourWon modify”);
} catch (IOException io) {
io.printStackTrace();
} finally {
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
输出结果,在resources目录下多一个文件config.properties,内容如下
#JourWon modify
#Mon Sep 09 14:23:44 CST 2019
password=123456
username=root
读取
下面给出常见的六种读取properties文件的方式:
public static void main(String[] args) throws IOException {
// PropertiesTest.getPath1();
// PropertiesTest.getPath2();
// PropertiesTest.getPath3();
// PropertiesTest.getPath4();
// PropertiesTest.getPath5();
PropertiesTest.getPath6();
}
/**
-
一、 使用java.util.Properties类的load(InputStream in)方法加载properties文件
-
主要是需要加上src这个文件夹名。路径配置需要精确到绝对地址级别
-
@return
*/
public static void getPath1() throws IOException {
InputStream in = new BufferedInputStream(new FileInputStream(
new File(“src/main/resources/prop.properties”)));
printKeyValue(in);
}
/**
-
二、 使用java.util.ResourceBundle类的getBundle()方法
-
注意:这个getBundle()方法的参数只能写成包路径+properties文件名,注意不需要带上后缀名。
-
@return
*/
public static void getPath2() {
ResourceBundle rb = ResourceBundle
.getBundle(“prop”);
printKeyValueRb(rb);
}
/**
-
三、 使用java.util.PropertyResourceBundle类的构造函数
-
@return
*/
public static void getPath3() throws IOException {
InputStream in = new BufferedInputStream(new FileInputStream(“src/main/resources/prop.properties”));
ResourceBundle rb = new PropertyResourceBundle(in);
printKeyValueRb(rb);
}
/**
-
四、 使用class变量的getResourceAsStream()方法
-
注意:getResourceAsStream()方法的参数按格式写到包路径+properties文件名+.后缀
-
@return
*/
public static void getPath4() throws IOException {
InputStream in = PropertiesTest.class
.getResourceAsStream(“/prop.properties”);
printKeyValue(in);
}
/**
-
五、
-
使用class.getClassLoader()所得到的java.lang.ClassLoader的
-
getResourceAsStream()方法
-
getResourceAsStream(name)方法的参数必须是包路径+文件名+.后缀
-
否则会报空指针异常
-
@return
*/
public static void getPath5() throws IOException {
InputStream in = PropertiesTest.class.getClassLoader()
.getResourceAsStream(“./././prop.properties”);
printKeyValue(in);
}
/**
-
六、 使用java.lang.ClassLoader类的getSystemResourceAsStream()静态方法
-
getSystemResourceAsStream()方法的参数格式也是有固定要求的
-
@return
*/
public static void getPath6() throws IOException {
InputStream in = ClassLoader
.getSystemResourceAsStream(“./././prop.properties”);
printKeyValue(in);
}
/**
-
单独抽取的方法,用户检测能否正确操纵Properties
-
@param inputStream
-
@throws IOException
*/
public static void printKeyValue(InputStream inputStream) throws IOException {
Properties properties = new Properties();
properties.load(inputStream);
Set keys = properties.keySet();
for (Object key : keys) {
System.out.println(key + " = " + properties.get(key));
}
if (inputStream != null) {
inputStream.close();
}
}
public static void printKeyValueRb(ResourceBundle rb) {
Set keys = rb.keySet();
for (String key : keys) {
System.out.println(key + " = " + rb.getString(key));
}
}
输出结果都是
password = 123456
username = root
其中第一、四、五、六种方式都是先获得文件的输入流,然后通过Properties类的load(InputStream inStream)方法加载到Properties对象中,最后通过Properties对象来操作文件内容。
第二、三中方式是通过ResourceBundle类来加载Properties文件,然后ResourceBundle对象来操做properties文件内容。
其中最重要的就是每种方式加载文件时,文件的路径需要按照方法的定义的格式来加载,否则会抛出各种异常,比如空指针异常。
遍历
下面给出四种遍历Properties中的所有键值对的方法:
/**
- 输出properties的key和value
*/
public static void printProp(Properties properties) {
System.out.println(“---------(方式一)------------”);
for (String key : properties.stringPropertyNames()) {
System.out.println(key + “=” + properties.getProperty(key));
}
System.out.println(“---------(方式二)------------”);
//返回属性key的集合
Set keys = properties.keySet();
最后
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Java工程师,想要提升技能,往往是自己摸索成长,自己不成体系的自学效果低效漫长且无助。
因此收集整理了一份《2024年Java开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!
转存中…(img-5POzojLr-1715868211064)]
[外链图片转存中…(img-8fCFVbqb-1715868211065)]
[外链图片转存中…(img-hqT0ODXx-1715868211065)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Java开发知识点,不论你是刚入门Java开发的新手,还是希望在技术上不断提升的资深开发者,这些资料都将为你打开新的学习之门!
如果你觉得这些内容对你有帮助,需要这份全套学习资料的朋友可以戳我获取!!
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!