在java項目中,操做properties文件是常常要作的,由於不少的配置信息都會寫在properties文件中,這裏主要是總結使用getResourceAsStream方法和InputStream流去讀取properties文件,使用getResourceAsStream方法去讀取properties文件時須要特別注意properties文件路徑的寫法,測試項目以下:java
/* 範例名稱:java讀取properties文件總結 * 源文件名稱:PropertiesFileReadTest.java * 要 點: * 1. 使用getResourceAsStream方法讀取properties文件 * 2. 使用InPutStream流讀取properties文件 * 3. 讀取properties文件的路徑寫法問題 * 時間:2014/4/2 */ package propertiesFile.read.test; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.text.MessageFormat; import java.util.Properties; public class PropertiesFileReadTest { /** * @param args */ public static void main(String[] args) { try { readPropFileByGetResourceAsStream(); System.out.println(""); readPropFileByInPutStream(); } catch (Exception e) { e.printStackTrace();// TODO: handle exception } } /** * 使用getResourceAsStream方法讀取properties文件 */ static void readPropFileByGetResourceAsStream() { /** * 讀取src下面config.properties包內的配置文件 * test1.properties位於config.properties包內 */ InputStream in1 = PropertiesFileReadTest.class.getClassLoader() .getResourceAsStream("config/properties/test1.properties"); /** * 讀取和PropertiesFileReadTest類位於同一個包裏面的配置文件 * test2.properties和PropertiesFileReadTest類在同一個包裏面 */ InputStream in2 = PropertiesFileReadTest.class .getResourceAsStream("test2.properties"); /** * 讀取src根目錄下文件的配置文件 * jdbc.properties位於src目錄 */ InputStream in3 = PropertiesFileReadTest.class.getClassLoader() .getResourceAsStream("jdbc.properties"); /** * 讀取位於另外一個source文件夾裏面的配置文件 * config是一個source文件夾,config.properties位於config source文件夾中 */ InputStream in4 = PropertiesFileReadTest.class.getClassLoader() .getResourceAsStream("config.properties"); Properties p = new Properties(); System.out.println("----使用getResourceAsStream方法讀取properties文件----"); try { System.out .println("----------------------------------------------"); p.load(in1); System.out.println("test1.properties:name=" + p.getProperty("name") + ",age=" + p.getProperty("age")); System.out .println("----------------------------------------------"); p.load(in2); System.out.println("test2.properties:name=" + p.getProperty("name") + ",age=" + p.getProperty("age")); System.out .println("----------------------------------------------"); p.load(in3); System.out.println("jdbc.properties:"); System.out.println(String.format("jdbc.driver=%s", p.getProperty("jdbc.driver")));// 這裏的%s是java String佔位符 System.out.println(String.format("jdbc.url=%s", p.getProperty("jdbc.url"))); System.out.println(String.format("jdbc.usename=%s", p.getProperty("jdbc.usename"))); System.out.println(String.format("jdbc.password=%s", p.getProperty("jdbc.password"))); System.out .println("----------------------------------------------"); p.load(in4); System.out.println("config.properties:"); System.out.println(MessageFormat.format("dbuser={0}", p.getProperty("dbuser")));// {0}是一個java的字符串佔位符 System.out.println(MessageFormat.format("dbpassword={0}", p.getProperty("dbpassword"))); System.out.println(MessageFormat.format("database={0}", p.getProperty("database"))); System.out .println("----------------------------------------------"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { if (in1 != null) { try { in1.close(); } catch (IOException e) { e.printStackTrace(); } } if (in2 != null) { try { in2.close(); } catch (IOException e) { e.printStackTrace(); } } if (in3 != null) { try { in3.close(); } catch (IOException e) { e.printStackTrace(); } } if (in4 != null) { try { in4.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 使用InPutStream流讀取properties文件 */ static void readPropFileByInPutStream() { InputStream in1 = null; InputStream in2 = null; InputStream in3 = null; InputStream in4 = null; System.out.println("----使用InputStream流讀取properties文件----"); try { /** * 讀取src根目錄下文件的配置文件 * jdbc.properties位於src目錄 */ in1 = new BufferedInputStream(new FileInputStream( "src/jdbc.properties")); /** * 讀取src下面config.properties包內的配置文件 * test1.properties位於config.properties包內 */ in2 = new BufferedInputStream(new FileInputStream( "src/config/properties/test1.properties")); /** * 讀取和PropertiesFileReadTest類位於同一個包裏面的配置文件 * test2.properties和PropertiesFileReadTest類在同一個包裏面 */ in3 = new BufferedInputStream(new FileInputStream( "src/propertiesFile/read/test/test2.properties")); /** * 讀取位於另外一個source文件夾裏面的配置文件 * config是一個source文件夾,config.properties位於config source文件夾中 */ in4 = new FileInputStream("config/config.properties"); Properties p = new Properties(); System.out .println("----------------------------------------------"); p.load(in1); System.out.println("jdbc.properties:"); System.out.println(String.format("jdbc.driver=%s", p.getProperty("jdbc.driver")));// 這裏的%s是java String佔位符 System.out.println(String.format("jdbc.url=%s", p.getProperty("jdbc.url"))); System.out.println(String.format("jdbc.usename=%s", p.getProperty("jdbc.usename"))); System.out.println(String.format("jdbc.password=%s", p.getProperty("jdbc.password"))); System.out .println("----------------------------------------------"); p.load(in2); System.out.println("test1.properties:name=" + p.getProperty("name") + ",age=" + p.getProperty("age")); System.out .println("----------------------------------------------"); p.load(in3); System.out.println("test2.properties:name=" + p.getProperty("name") + ",age=" + p.getProperty("age")); System.out .println("----------------------------------------------"); p.load(in4); System.out.println("config.properties:"); System.out.println(MessageFormat.format("dbuser={0}", p.getProperty("dbuser")));// {0}是一個java的字符串佔位符 System.out.println(MessageFormat.format("dbpassword={0}", p.getProperty("dbpassword"))); System.out.println(MessageFormat.format("database={0}", p.getProperty("database"))); System.out .println("----------------------------------------------"); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (in1 != null) { try { in1.close(); } catch (IOException e) { e.printStackTrace(); } } if (in2 != null) { try { in2.close(); } catch (IOException e) { e.printStackTrace(); } } if (in3 != null) { try { in3.close(); } catch (IOException e) { e.printStackTrace(); } } if (in4 != null) { try { in4.close(); } catch (IOException e) { e.printStackTrace(); } } } } }
運行結果:測試
項目demo下載url