属性文件:用字符串表示的"key=value",可以通过Properties类方便地完成对属性文件的操作。
Properties类的主要方法:
public Properties():构造一个空的属性类。
public Properties(Properties defaults): 构造一个指定属性内容的属性类。
public String getProperty(String key): 根据key取得value,没有返回null
public String getProperty(String key, String defaultValue) :根据key取得value,如果没有key返回defaultValue
public Object setProperties(String key, String value) : 设置属性
public void list(PrintStream inStream) :属性打印
public void load(InputStram inStream) throws IOException : 从输入流中取出全部的属性内容
public void loadFromXML(InputStream in) throws IOException,InvalidPropertiesFormatException:从xml文件格式中读取内容
public void store(OutputStream out,String comments) throws IOException :将属性内容通过输出流输出,同时声明属性的注释
public void storeToXML(OutputStream os,String comment) throws IOException :以xml文件格式输出属性,默认编码
public void storeToXML(OutputStream os,String comment, String encoding) throws IOException:以xml文件格式输出属性,用户指定默认编码
例子1:设置取得属性
import java.util.Properties;
public class PropertiesDemo01{
public static void main(String args[]){
Properties pro = new Properties() ; // 创建Properties对象
pro.setProperty("BJ","BeiJing") ; // 设置属性
pro.setProperty("TJ","TianJin") ;
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","没有发现")) ;
}
};
运行结果:
1、BJ属性存在:BeiJing
2、SC属性不存在:null
3、SC属性不存在,同时设置显示的默认值:没有发现
例子2:将属性保存到普通文件中
import java.util.Properties;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class PropertiesDemo02{
public static void main(String args[]){
Properties pro = new Properties() ; // 创建Properties对象
pro.setProperty("BJ","BeiJing") ; // 设置属性
pro.setProperty("TJ","TianJin") ;
pro.setProperty("NJ","NanJing") ;
File file = new File("D:" + File.separator + "area.properteis") ; // 指定要操作的文件
try{
pro.store(new FileOutputStream(file),"Area Info") ; // 保存属性到普通文件
}catch(FileNotFoundException e){
e.printStackTrace() ;
}catch(IOException e){
e.printStackTrace() ;
}
}
};
运行结果:
#Area Info
#Sun Jul 17 09:56:17 CST 2016
BJ=BeiJing
NJ=NanJing
TJ=TianJin
例子3:从属性文件中读取内容
import java.util.Properties;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class PropertiesDemo03{
public static void main(String args[]){
Properties pro = new Properties() ; // 创建Properties对象
File file = new File("D:" + File.separator + "area.properteis") ; // 指定要操作的文件
try{
pro.load(new FileInputStream(file)) ; // 读取属性文件
}catch(FileNotFoundException e){
e.printStackTrace() ;
}catch(IOException e){
e.printStackTrace() ;
}
System.out.println("1、BJ属性存在:" + pro.getProperty("BJ")) ;
System.out.println("2、SH属性存在:" + pro.getProperty("SH")) ;
}
};
运行结果:1、BJ属性存在:BeiJing
2、SH属性存在:null
例子4:将属性信息保存到xml文件中国
import java.util.Properties;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class PropertiesDemo04{
public static void main(String args[]){
Properties pro = new Properties() ; // 创建Properties对象
pro.setProperty("BJ","BeiJing") ; // 设置属性
pro.setProperty("TJ","TianJin") ;
pro.setProperty("NJ","NanJing") ;
File file = new File("D:" + File.separator + "area.xml") ; // 指定要操作的文件
try{
pro.storeToXML(new FileOutputStream(file),"Area Info") ; // 保存属性到普通文件
}catch(FileNotFoundException e){
e.printStackTrace() ;
}catch(IOException e){
e.printStackTrace() ;
}
}
};
运行结果:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Area Info</comment>
<entry key="BJ">BeiJing</entry>
<entry key="NJ">NanJing</entry>
<entry key="TJ">TianJin</entry>
</properties>
例子5:从xml文件中读取属性
import java.util.Properties;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
public class PropertiesDemo05{
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(FileNotFoundException e){
e.printStackTrace() ;
}catch(IOException e){
e.printStackTrace() ;
}
System.out.println("1、BJ属性存在:" + pro.getProperty("BJ")) ;
}
};
运行结果:1、BJ属性存在:BeiJing