Properties类表示了一个持久的属性集。Properties 可保存在流中或从流中加载。可用来读取和设置配置文件如.xml文件,.properties文件实例如下: package test; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.InvalidPropertiesFormatException; import java.util.Properties; public class TestProperties { public static final String DB_DRIVERNAME="driverName"; public static final String DB_URL="url"; public static final String DB_USERNAME="username"; public static final String DB_PWD="pwd"; public static final String SYSTEMFILE_PROPERTIES="webapps/WEB-INF/classes/test/tp.properties"; //路径视实际情况而定,参加令一篇文章 http://liujunhg.iteye.com/blog/1039168 (java中路径的问题) public static final String SYSTEMFILE_PROPERTIES2="webapps/WEB-INF/classes/test/tp2.properties"; public static final String SYSTEMFILE_XML="webapps/WEB-INF/classes/test/tp.xml"; public static final String SYSTEMFILE_XML2="webapps/WEB-INF/classes/test/tp2.xml"; protected Properties properties=null; public TestProperties(){ //this(SYSTEMFILE_PROPERTIES); } private TestProperties(String fileName){ //loadProperties(SYSTEMFILE_PROPERTIES); this.loadXML(SYSTEMFILE_XML); } //解析properties文件 public void loadProperties(String fileName){ File file=new File(fileName); InputStream in=null; properties=new Properties(); if(file.exists()){ try { in=new FileInputStream(file); properties.load(in); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ if(in!=null){ try { in.close(); } catch (IOException e) { e.printStackTrace(); } in=null; } } }else{ System.out.println("文件不存在!"); } } //解析XML文件 public void loadXML(String filename){ File file=new File(SYSTEMFILE_XML); InputStream in=null; properties=new Properties(); if(file.exists()){ try { in=new FileInputStream(file); properties.loadFromXML(in); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (InvalidPropertiesFormatException e) { e.printStackTrace(); } catch (IOException e) { if(in!=null){ try { in.close(); } catch (IOException e1) { e1.printStackTrace(); } in=null; } e.printStackTrace(); } }else{ System.out.println("文件不存在!"); } } //设置properties文件 public void storeProperties(){ properties=new Properties(); properties.setProperty(DB_DRIVERNAME, "oracle.jdbc.driver.OracleDriver"); properties.setProperty(DB_URL, "jdbc:oracle:thin@127.0.0.1:1521:orcl"); // properties.setProperty(DB_USERNAME, "test2"); // properties.setProperty(DB_PWD, "test2"); File file=new File(SYSTEMFILE_PROPERTIES2); FileOutputStream fo=null; try { fo=new FileOutputStream(file); properties.store(fo, "test"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ if(fo!=null){ try { fo.close(); } catch (IOException e) { e.printStackTrace(); } fo=null; } } } //设置XML文件 public void storeXML(){ properties=new Properties(); properties.setProperty(DB_DRIVERNAME, "oracle.jdbc.driver.OracleDriver"); properties.setProperty(DB_URL, "jdbc:oracle:thin@127.0.0.1:1521:orcl"); properties.setProperty(DB_USERNAME, "test2"); properties.setProperty(DB_PWD, "test2"); File file=new File(SYSTEMFILE_XML2); FileOutputStream fo=null; try { fo=new FileOutputStream(file); properties.storeToXML(fo, "conn"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }finally{ if(fo!=null){ try { fo.close(); } catch (IOException e) { e.printStackTrace(); } fo=null; } } } public String getDrivername(){ return (String)properties.getProperty(DB_DRIVERNAME); } public String getURL(){ return (String)properties.getProperty(DB_URL); } public String getName(){ return (String)properties.getProperty(DB_USERNAME); } public String getPWD(){ return (String)properties.getProperty(DB_PWD); } public static void main(String args[]){ TestProperties tp=new TestProperties(); // System.out.println(tp.getDrivername()); // System.out.println(tp.getURL()); // System.out.println(tp.getName()); // System.out.println(tp.getPWD()); //tp.storeXML(); tp.storeProperties(); } } tp.properties:url=jdbc:oracle:thin@127.0.0.1:1521:orcldriverName=oracle.jdbc.driver.OracleDriverusername=123pwd=123tp.xml<?xml version="1.0" encoding="UTF-8" standalone="no"?><!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"><properties><comment>Hi</comment><entry key="url">jdbc:oracle:thin@127.0.0.1:1521:orcl</entry><entry key="driverName">oracle.jdbc.driver.OracleDriver</entry><entry key="username">123</entry><entry key="pwd">123</entry></properties>