一、Properties
1、类源码:Properties 继承 HashTable,属于一个持久的属性集合,以 key-value 键值对形式存在
class Properties extends Hashtable<Object,Object> {
2、类方法:除了HashTable定义的方法,Properties定义了下列方法,表格来源——菜鸟教程
序号 | 方法描述 |
---|---|
1 | String getProperty(String key) 用指定的键在此属性列表中搜索属性。 |
2 | String getProperty(String key, String defaultProperty) 用指定的键在属性列表中搜索属性。 |
3 | void list(PrintStream streamOut) 将属性列表输出到指定的输出流。 |
4 | void list(PrintWriter streamOut) 将属性列表输出到指定的输出流。 |
5 | void load(InputStream streamIn) throws IOException 从输入流中读取属性列表(键和元素对)。 |
6 | Enumeration propertyNames( ) 按简单的面向行的格式从输入字符流中读取属性列表(键和元素对)。 |
7 | Object setProperty(String key, String value) 调用 Hashtable 的方法 put。 |
8 | void store(OutputStream streamOut, String description) 以适合使用 load(InputStream)方法加载到 Properties 表中的格式,将此 Properties 表中的属性列表(键和元素对)写入输出流。 |
注:本篇博客中 data1.properties 路径:/src/data1.properties;data2.properties 路径:/src/IO/data2.properties
3、代码示例——写入
package IO;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.Properties;
public class PropertiesDemo {
public static void main(String[] args) {
Properties props = new Properties();
OutputStream os = null;
//获取PropertiesDemo类的根路径
URL base = PropertiesDemo.class.getResource("/");
String filepath = base.getPath() + "IO/data2.properties";
try {
os = new FileOutputStream(filepath);
//键值对
props.setProperty("key1", "test");
props.setProperty("key2", "test");
//保存键值对
props.store(os, "test");
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
//关闭输出流
os.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
4、代码示例——读取
package IO;
import java.io.*;
import java.net.URL;
import java.util.Properties;
public class PropertiesDemo {
public static void main(String[] args) {
Properties props = new Properties();
InputStream in = null;
//获取PropertiesDemo类的根路径
URL base = PropertiesDemo.class.getResource("/");
String filepath = base.getPath() + "IO/data2.properties";
try {
//输入流
in = new FileInputStream(filepath);
props.load(in);
System.out.println(props.getProperty("key1"));
} catch (IOException e) {
System.out.println("Properties文件读取报错!");
e.printStackTrace();
} finally {
try {
//关闭输入流
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
5、其他读取方法
- java.util.ResourceBundle 类的getBundle() 方法
//使用ResourceBundle类的getBundle()方法
ResourceBundle rb=ResourceBundle.getBundle("IO/data2");
//输出key1对应value值
System.out.println(rb.getString("key1"));
- java.util.PropertyResourceBundle类的构造函数
//java.util.PropertyResourceBundle类的构造函数
ResourceBundle rb=new PropertyResourceBundle(in);
System.out.println(rb.getString("key1"));
注:博主比较常用的还是代码示例中的 props.load(in) 这种方式
二、相对路径和绝对路径
1、概念
- 绝对路径:文件/目录在硬盘上真正的路径
- 相对路径:指的是相对JVM的启动路径,其实java实际上都是通过绝对路径寻找资源的,相对路径是API通过底层拼接成绝对路径的。
2、分隔符:java 中通用 System.getProperty(File.separator),System 具备Properties属性
- Linux 下:" / "
- Windows 下:" \\ "
3、绝对路径读取文件
- D:\\1.properties (在windows下,转义为" \\ ")
- 以src为根目录读取
in=new FileInputStream("src/IO/data2.properties");
props.load(in);
4、相对路径读取文件
- class.getResource(""):包路径
//包路径是IO,IO下面是data2.properties
String filepath=PropertiesDemo.class.getResource("data2.properties").getPath();
- class.getResource("/"):classpath根路径
//获取PropertiesDemo类的根路径
URL base = PropertiesDemo.class.getResource("/");
String filepath = base.getPath() + "IO/data2.properties";
String filepath=PropertiesDemo.class.getResource("/data1.properties").getPath();
String filepath=PropertiesDemo.class.getResource("/IO/data2.properties").getPath();
- class.getClassLoader().getResource(""):包路径
String filepath=PropertiesDemo.class.getClassLoader().getResource("data1.properties").getPath();
String filepath=PropertiesDemo.class.getClassLoader().getResource("IO/data2.properties").getPath();