读取properties 文件
通过上述的 classloader 获取项目根路径 ,我们来加载 properties 文件
user=hjy
passwd=123456
public class App
{
public static void main( String[] args ) throws ClassNotFoundException, IOException {
// 随便 new 的一个对象 ,用来获取 classloader
hello hello = new hello();
// 相当于获取路径简化一步 ,直接获得输入流
InputStream resource = hello.getClass().getClassLoader().getResourceAsStream("test.properties");
Properties properties = new Properties();
// 加载输入流 ,这样我们就可以获取其中的键值对了
properties.load(resource);
System.out.println(properties.getProperty("user"));
System.out.println(properties.getProperty("passwd"));
}
}
当然 ,我们也可以使用原始的方法
public class App
{
public static void main( String[] args ) throws ClassNotFoundException, IOException {
// 随便 new 的一个对象 ,用来获取 classloader
hello hello = new hello();
// 获取根路径
String path = hello.getClass().getClassLoader().getResource("test.properties").getPath();
FileInputStream resource = new FileInputStream(path);
Properties properties = new Properties();
properties.load(resource);
System.out.println(properties.getProperty("user"));
System.out.println(properties.getProperty("passwd"));
}
}
设置properties 文件
首先 ,我们需要指定设置的值
properties.setProperty(key,value);
然后获取文件路径的 输入流 ,调用 以下代码 ,修改文件
properties.store(stream,null);
Properties properties = getProperties(location);
properties.setProperty(key,value);
FileOutputStream stream = null;
try {
stream = new FileOutputStream(Thread.currentThread().getContextClassLoader().getResource(location).getPath());
properties.store(stream,null);
} catch (IOException e) {
e.printStackTrace();
}
工具类
我们可以写一个工具类 ,来实现 properties 文件的读取的设置
package com.hjy.test;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class MyProperties {
/**
* 获取 properties 对象
* @param location
* @return
*/
public static Properties getProperties(String location) {
Properties properties = new Properties();
try {
properties.load(Thread.currentThread().getContextClassLoader().getResourceAsStream(location));
} catch (IOException e) {
e.printStackTrace();
}
return properties;
}
/**
* 获取 properties 文件的属性值
* @param location
* @param key
* @return
*/
public static String getProperty(String location,String key) {
Properties properties = getProperties(location);
return properties.getProperty(key);
}
/**
* 设置指定位置的 properties 文件的属性值
* @param location
* @param key
* @param value
*/
public static void setProperty(String location,String key ,String value) {
Properties properties = getProperties(location);
properties.setProperty(key,value);
FileOutputStream stream = null;
try {
stream = new FileOutputStream(Thread.currentThread().getContextClassLoader().getResource(location).getPath());
properties.store(stream,null);
} catch (IOException e) {
e.printStackTrace();
}
}
}
最后主函数中测试
public static void main(String[] args) throws IOException {
String name = MyProperties.getProperty("test.properties", "name");
System.out.println(name);
// 设置的是打包后的 properties 文件 ,而不是项目中的这个
MyProperties.setProperty("test.properties","name","hjy");
String name1 = MyProperties.getProperty("test.properties", "name");
System.out.println(name1);
}