1. 属性类 Properties 相关概念
- Properties 类主要是进行属性信息的操作,同时可以结合 IO 流实现属性操作的输出与读取操作,下面介绍具体概念。
- 在 Java 中属性类操作是一个较为重要的类。而要想明白属性操作类的使用,就必须先清楚什么事属性文件,实际上在国际化操作时就用到属性文件(Message.properties),在一个属性文件中保存了多个属性,每一个属性就是直接用字符串表示出来的 “key = value” 对,如果想要操作这些属性文件中的属性,就可以通过 Properties 类来方便的完成。通过 “key = value” 的形式保存的,这样的文件就是属性文件。
- Properties 类本身是 Hashtable 类的子类,既然是子类,则肯定也是按照 key 和 value 的形式存放数据的,Properties 类定义如下:
public class Properties extends Hashtable<Object,Object)
- Properties 类中的主要方法如表:
序号 | 方法 | 类型 | 描述 |
---|---|---|---|
1 | public Properties() | 构造 | 构造一个空的属性类 |
2 | public Properties(Properties default) | 常量 | 构造一个指定属性内容的属性类 |
3 | public String getProperty(String key) | 常量 | 根据属性的 key 取得属性的 value,如果没有 key 则返回 null |
4 | public String getProperty(String key, String defaultValue) | 普通 | 据属性的 key 取得属性的 value,如果没有 key 则返回 默认值 |
5 | public Object setProperty(String key, String value) | 普通 | 设置属性 |
6 | public void list( PrintStream out) | 普通 | 属性打印 |
7 | public void load( InputStream instream ) throws IOException | 普通 | 从输入流中取出全部的属性内容 |
8 | public void loadFromXML( InputStream instream ) throws IOException,InvalidPropertiesException | 普通 | 从 XML 文件格式中读取内容 |
9 | public void store( OutputStream out, String comments ) throws IOException | 普通 | 将属性内容通过输出流输出,同时声明属性的注释 |
10 | public void storeToXML( OutputStream os, String comment ) throws IOException | 普通 | 以 XML 文件格式输出属性,默认编码 |
11 | public void storeToXML( OutputStream os, String comment,String encoding ) throws IOException | 普通 | 以 XML 文件格式输出属性,用户指定默认编码 |
2. Properties 操作实例
2.1 设置和取得属性
可以使用 setProperty() 和 getProperty() 方法设置和取得属性,操作的时候要以 String 为操作类型。
实例 1 代码:
package self.learn.propertydemo;
import java.util.Properties;
public class PropertyDemo {
public static void main(String[] args) {
Properties pro = new Properties(); // 创建 Properties 对象
pro.setProperty("BJ", "BeJing"); // 设置内容
pro.setProperty("TJ", "TianJing");
pro.setProperty("NJ", "NanJing");
System.out.println("1、BJ 属性存在:"+pro.getProperty("BJ"));
System.out.println("2、SC 属性不存在:"+pro.getProperty("SC"));
System.out.println("3、SC 属性不存在,同时设置显示的默认值:"+pro.getProperty("SC","没有发现"));
}
}
运行结果属性:
2.2 将属性保存在普通文件
正常属性类操作完成后,可以将其内容保存在文件中,那么直接使用 store() 方法即可,同时指定 OutputStream 类型,指明输出的位置。属性文件的扩展名是任意的,但是最后按照标准,将属性文件的扩展名统一成“*.properties”。
实例 2 代码:
package self.learn.propertydemo;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;
public class PropertyDemo {
public static void main(String[] args) {
Properties pro = new Properties(); // 创建 Properties 对象
pro.setProperty("BJ", "BeJing"); // 设置内容
pro.setProperty("TJ", "TianJing");
pro.setProperty("NJ", "NanJing");
// 设置属性文件的保存路径
File file = new File("D:"+File.separator+"area.properties");
try {
// 保存属性文件到普通文件之中,并设置注释内容
pro.store(new FileOutputStream(file), "Area Info");
} catch (Exception e) {
e.printStackTrace();
System.out.println("保存属性文件到普通文件之中出现异常:"+e.getMessage());
}
}
}
运行结果属性:
2.3 从普通文件之中读取属性内容
既然可以保存,也可以通过 load() 方法,从输入流中将所保存的所有属性内容读取出来。
实例 3 代码:
package self.learn.propertydemo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
public class PropertyDemo {
public static void main(String[] args) {
Properties pro = new Properties(); // 创建 Properties 对象
// pro.setProperty("BJ", "BeJing"); // 设置内容
// pro.setProperty("TJ", "TianJing");
// pro.setProperty("NJ", "NanJing");
// 设置属性文件的保存路径
File file = new File("D:"+File.separator+"area.properties");
try {
pro.load(new FileInputStream(file)); // 读取属性文件内容
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("BJ 属性值存在,内容是:"+pro.getProperty("BJ"));
}
}
运行结果属性:
2.4 将属性保存在 XML 文件中
在 Properties 类中也可以把全部内容以 XML 格式通过输出流输出,如果要把属性保存在 XML 文件中,则文件的扩展名最后为“*.xml”。
实例 4 代码:
package self.learn.propertydemo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
public class PropertyDemo {
public static void main(String[] args) {
Properties pro = new Properties(); // 创建 Properties 对象
pro.setProperty("BJ", "BeJing"); // 设置内容
pro.setProperty("TJ", "TianJing");
pro.setProperty("NJ", "NanJing");
// 设置属性文件的保存路径
File file = new File("D:"+File.separator+"area.xml");
try {
pro.storeToXML(new FileOutputStream(file),"Area Info"); // 读取属性文件内容
} catch (Exception e) {
e.printStackTrace();
}
//System.out.println("BJ 属性值存在,内容是:"+pro.getProperty("BJ"));
}
}
运行结果属性:
2.5 从 XML 文件中读取属性
以 XML 文件格式输出的全部属性后,必须要使用 loadFromXML() 方法将内容读取进来。
实例 5 代码:
package self.learn.propertydemo;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;
public class PropertyDemo {
public static void main(String[] args) {
Properties pro = new Properties(); // 创建 Properties 对象
// 设置属性文件的保存路径
File file = new File("D:"+File.separator+"area.xml");
try {
pro.loadFromXML(new FileInputStream(file)); // 读取属性文件内容
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("BJ 属性值存在,内容是:"+pro.getProperty("BJ"));
}
}