在一个属性文件中保存了多个属性,每一个属性就是直接用字符串表示出来的"key=value对",而如果想要轻松地操作这些属性文件中的属性,可以通过Properties类方便地完成。
<1>设置和取得属性
import java.util.Properties;//=================================================
// File Name : Properties_demo
//------------------------------------------------------------------------------
// Author : Common//主类
//Function : Properties_demo;
public class Properties_demo {public static void main(String[] args) {// TODO 自动生成的方法存根Properties pro = new Properties();pro.setProperty("BJ", "Beijing");pro.setProperty("NJ", "Nanjing");pro.setProperty("TJ", "Tianjin");System.out.println("获得属性"+pro.getProperty("BJ"));System.out.println("获得属性不存在"+pro.getProperty("HB"));System.out.println("获得属性不存在,同时设置默认的显示值"+pro.getProperty("HB",":::没有发现"));}}
<2>将属性保存在普通文件中
import java.io.File;
import java.io.FileOutputStream;
import java.util.Properties;//=================================================
// File Name : Properties_demo
//------------------------------------------------------------------------------
// Author : Common//主类
//Function : Properties_demo;
public class Properties_demo {public static void main(String[] args) {// TODO 自动生成的方法存根Properties pro = new Properties();pro.setProperty("BJ", "Beijing");pro.setProperty("NJ", "Nanjing");pro.setProperty("TJ", "Tianjin");System.out.println("获得属性"+pro.getProperty("BJ"));System.out.println("获得属性不存在"+pro.getProperty("HB"));System.out.println("获得属性不存在,同时设置默认的显示值"+pro.getProperty("HB",":::没有发现"));File f = new File("/home/common/software/coding/HelloWord/HelloWord/test.properties");//路径try{pro.store(new FileOutputStream(f),"pro info"); //保存并添加注释信息}catch(Exception e){e.printStackTrace();}}}
<3>从普通文件中读取属性内容
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;//=================================================
// File Name : Properties_demo
//------------------------------------------------------------------------------
// Author : Common//主类
//Function : Properties_demo;
public class Properties_demo {public static void main(String[] args) {// TODO 自动生成的方法存根Properties pro = new Properties();pro.setProperty("BJ", "Beijing");pro.setProperty("NJ", "Nanjing");pro.setProperty("TJ", "Tianjin");System.out.println("获得属性"+pro.getProperty("BJ"));System.out.println("获得属性不存在"+pro.getProperty("HB"));System.out.println("获得属性不存在,同时设置默认的显示值"+pro.getProperty("HB",":::没有发现"));File f = new File("/home/common/software/coding/HelloWord/HelloWord/test.properties");//路径try{pro.load(new FileInputStream(f)); //读取属性文件}catch(Exception e){e.printStackTrace();}System.out.println("BJ属性值为"+pro.getProperty("BJ")); }}
<4>将属性保存在XML文件中
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;//=================================================
// File Name : Properties_demo
//------------------------------------------------------------------------------
// Author : Common//主类
//Function : Properties_demo;
public class Properties_demo {public static void main(String[] args) {// TODO 自动生成的方法存根Properties pro = new Properties();pro.setProperty("BJ", "Beijing");pro.setProperty("NJ", "Nanjing");pro.setProperty("TJ", "Tianjin");System.out.println("获得属性"+pro.getProperty("BJ"));System.out.println("获得属性不存在"+pro.getProperty("HB"));System.out.println("获得属性不存在,同时设置默认的显示值"+pro.getProperty("HB",":::没有发现"));File f = new File("/home/common/software/coding/HelloWord/HelloWord/test.xml");//路径try{pro.storeToXML(new FileOutputStream(f),"pro info"); //保存并添加注释信息}catch(Exception e){e.printStackTrace();}System.out.println("BJ属性值为"+pro.getProperty("BJ")); }}
<5>从XML文件中读取属性
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Properties;//=================================================
// File Name : Properties_demo
//------------------------------------------------------------------------------
// Author : Common//主类
//Function : Properties_demo;
public class Properties_demo {public static void main(String[] args) {// TODO 自动生成的方法存根Properties pro = new Properties();pro.setProperty("BJ", "Beijing");pro.setProperty("NJ", "Nanjing");pro.setProperty("TJ", "Tianjin");System.out.println("获得属性"+pro.getProperty("BJ"));System.out.println("获得属性不存在"+pro.getProperty("HB"));System.out.println("获得属性不存在,同时设置默认的显示值"+pro.getProperty("HB",":::没有发现"));File f = new File("/home/common/software/coding/HelloWord/HelloWord/test.xml");//路径try{pro.loadFromXML(new FileInputStream(f)); //读取属性文件}catch(Exception e){e.printStackTrace();}System.out.println("BJ属性值为"+pro.getProperty("BJ")); }}