Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1661696
  • 博文数量: 695
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 4027
  • 用 户 组: 普通用户
  • 注册时间: 2013-11-20 21:22
文章分类

全部博文(695)

文章存档

2018年(18)

2017年(74)

2016年(170)

2015年(102)

2014年(276)

2013年(55)

分类: Java

2016-10-11 15:10:11

一、java读取properties文件总结

  在java项目中,操作properties文件是经常要做的,因为很多的配置信息都会写在properties文件中,这里主要是总结使用getResourceAsStream方法和InputStream流去读取properties文件,使用getResourceAsStream方法去读取properties文件时需要特别注意properties文件路径的写法,测试项目如下:

1.1.项目的目录结构



点击(此处)折叠或打开

  1. /* 范例名称:java读取properties文件总结
  2.  * 源文件名称:PropertiesFileReadTest.java
  3.  * 要 点:
  4.  * 1. 使用getResourceAsStream方法读取properties文件
  5.  * 2. 使用InPutStream流读取properties文件
  6.  * 3. 读取properties文件的路径写法问题
  7.  * 时间:2014/4/2
  8.  */
  9. package propertiesFile.read.test;

  10. import java.io.BufferedInputStream;
  11. import java.io.FileInputStream;
  12. import java.io.FileNotFoundException;
  13. import java.io.IOException;
  14. import java.io.InputStream;
  15. import java.text.MessageFormat;
  16. import java.util.Properties;

  17. public class PropertiesFileReadTest {

  18.     /**
  19.      * @param args
  20.      */
  21.     public static void main(String[] args) {
  22.         try {
  23.             readPropFileByGetResourceAsStream();
  24.             System.out.println("");
  25.             readPropFileByInPutStream();
  26.         } catch (Exception e) {
  27.             e.printStackTrace();// TODO: handle exception
  28.         }
  29.     }

  30.     /**
  31.      * 使用getResourceAsStream方法读取properties文件
  32.      */
  33.     static void readPropFileByGetResourceAsStream() {
  34.         /**
  35.          * 读取src下面config.properties包内的配置文件
  36.          * test1.properties位于config.properties包内
  37.          */
  38.         InputStream in1 = PropertiesFileReadTest.class.getClassLoader()
  39.                 .getResourceAsStream("config/properties/test1.properties");
  40.         /**
  41.          * 读取和PropertiesFileReadTest类位于同一个包里面的配置文件
  42.          * test2.properties和PropertiesFileReadTest类在同一个包里面
  43.          */
  44.         InputStream in2 = PropertiesFileReadTest.class
  45.                 .getResourceAsStream("test2.properties");
  46.         /**
  47.          * 读取src根目录下文件的配置文件
  48.          * jdbc.properties位于src目录
  49.          */
  50.         InputStream in3 = PropertiesFileReadTest.class.getClassLoader()
  51.                 .getResourceAsStream("jdbc.properties");
  52.         /**
  53.          * 读取位于另一个source文件夹里面的配置文件
  54.          * config是一个source文件夹,config.properties位于config source文件夹中
  55.          */
  56.         InputStream in4 = PropertiesFileReadTest.class.getClassLoader()
  57.                 .getResourceAsStream("config.properties");

  58.         Properties p = new Properties();
  59.         System.out.println("----使用getResourceAsStream方法读取properties文件----");
  60.         try {
  61.             System.out
  62.                     .println("----------------------------------------------");
  63.             p.load(in1);
  64.             System.out.println("test1.properties:name=" + p.getProperty("name")
  65.                     + ",age=" + p.getProperty("age"));
  66.             System.out
  67.                     .println("----------------------------------------------");

  68.             p.load(in2);
  69.             System.out.println("test2.properties:name=" + p.getProperty("name")
  70.                     + ",age=" + p.getProperty("age"));
  71.             System.out
  72.                     .println("----------------------------------------------");

  73.             p.load(in3);
  74.             System.out.println("jdbc.properties:");
  75.             System.out.println(String.format("jdbc.driver=%s",
  76.                     p.getProperty("jdbc.driver")));// 这里的%s是java String占位符
  77.             System.out.println(String.format("jdbc.url=%s",
  78.                     p.getProperty("jdbc.url")));
  79.             System.out.println(String.format("jdbc.usename=%s",
  80.                     p.getProperty("jdbc.usename")));
  81.             System.out.println(String.format("jdbc.password=%s",
  82.                     p.getProperty("jdbc.password")));
  83.             System.out
  84.                     .println("----------------------------------------------");

  85.             p.load(in4);
  86.             System.out.println("config.properties:");
  87.             System.out.println(MessageFormat.format("dbuser={0}",
  88.                     p.getProperty("dbuser")));// {0}是一个java的字符串占位符
  89.             System.out.println(MessageFormat.format("dbpassword={0}",
  90.                     p.getProperty("dbpassword")));
  91.             System.out.println(MessageFormat.format("database={0}",
  92.                     p.getProperty("database")));
  93.             System.out
  94.                     .println("----------------------------------------------");
  95.         } catch (IOException e) {
  96.             // TODO Auto-generated catch block
  97.             e.printStackTrace();
  98.         } finally {
  99.             if (in1 != null) {
  100.                 try {
  101.                     in1.close();
  102.                 } catch (IOException e) {
  103.                     e.printStackTrace();
  104.                 }
  105.             }

  106.             if (in2 != null) {
  107.                 try {
  108.                     in2.close();
  109.                 } catch (IOException e) {
  110.                     e.printStackTrace();
  111.                 }
  112.             }

  113.             if (in3 != null) {
  114.                 try {
  115.                     in3.close();
  116.                 } catch (IOException e) {
  117.                     e.printStackTrace();
  118.                 }
  119.             }

  120.             if (in4 != null) {
  121.                 try {
  122.                     in4.close();
  123.                 } catch (IOException e) {
  124.                     e.printStackTrace();
  125.                 }
  126.             }
  127.         }
  128.     }

  129.     /**
  130.      * 使用InPutStream流读取properties文件
  131.      */
  132.     static void readPropFileByInPutStream() {
  133.         InputStream in1 = null;
  134.         InputStream in2 = null;
  135.         InputStream in3 = null;
  136.         InputStream in4 = null;
  137.         System.out.println("----使用InputStream流读取properties文件----");
  138.         try {
  139.             /**
  140.              * 读取src根目录下文件的配置文件
  141.              * jdbc.properties位于src目录
  142.              */
  143.             in1 = new BufferedInputStream(new FileInputStream(
  144.                     "src/jdbc.properties"));
  145.             /**
  146.              * 读取src下面config.properties包内的配置文件
  147.              * test1.properties位于config.properties包内
  148.              */
  149.             in2 = new BufferedInputStream(new FileInputStream(
  150.                     "src/config/properties/test1.properties"));
  151.             /**
  152.              * 读取和PropertiesFileReadTest类位于同一个包里面的配置文件
  153.              * test2.properties和PropertiesFileReadTest类在同一个包里面
  154.              */
  155.             in3 = new BufferedInputStream(new FileInputStream(
  156.                     "src/propertiesFile/read/test/test2.properties"));
  157.             /**
  158.              * 读取位于另一个source文件夹里面的配置文件
  159.              * config是一个source文件夹,config.properties位于config source文件夹中
  160.              */
  161.             in4 = new FileInputStream("config/config.properties");

  162.             Properties p = new Properties();
  163.             System.out
  164.                     .println("----------------------------------------------");

  165.             p.load(in1);
  166.             System.out.println("jdbc.properties:");
  167.             System.out.println(String.format("jdbc.driver=%s",
  168.                     p.getProperty("jdbc.driver")));// 这里的%s是java String占位符
  169.             System.out.println(String.format("jdbc.url=%s",
  170.                     p.getProperty("jdbc.url")));
  171.             System.out.println(String.format("jdbc.usename=%s",
  172.                     p.getProperty("jdbc.usename")));
  173.             System.out.println(String.format("jdbc.password=%s",
  174.                     p.getProperty("jdbc.password")));
  175.             System.out
  176.                     .println("----------------------------------------------");

  177.             p.load(in2);
  178.             System.out.println("test1.properties:name=" + p.getProperty("name")
  179.                     + ",age=" + p.getProperty("age"));
  180.             System.out
  181.                     .println("----------------------------------------------");
  182.             p.load(in3);
  183.             System.out.println("test2.properties:name=" + p.getProperty("name")
  184.                     + ",age=" + p.getProperty("age"));
  185.             System.out
  186.                     .println("----------------------------------------------");

  187.             p.load(in4);
  188.             System.out.println("config.properties:");
  189.             System.out.println(MessageFormat.format("dbuser={0}",
  190.                     p.getProperty("dbuser")));// {0}是一个java的字符串占位符
  191.             System.out.println(MessageFormat.format("dbpassword={0}",
  192.                     p.getProperty("dbpassword")));
  193.             System.out.println(MessageFormat.format("database={0}",
  194.                     p.getProperty("database")));
  195.             System.out
  196.                     .println("----------------------------------------------");
  197.         } catch (FileNotFoundException e) {
  198.             e.printStackTrace();
  199.         } catch (IOException e) {
  200.             e.printStackTrace();
  201.         } finally {
  202.             if (in1 != null) {
  203.                 try {
  204.                     in1.close();
  205.                 } catch (IOException e) {
  206.                     e.printStackTrace();
  207.                 }
  208.             }

  209.             if (in2 != null) {
  210.                 try {
  211.                     in2.close();
  212.                 } catch (IOException e) {
  213.                     e.printStackTrace();
  214.                 }
  215.             }

  216.             if (in3 != null) {
  217.                 try {
  218.                     in3.close();
  219.                 } catch (IOException e) {
  220.                     e.printStackTrace();
  221.                 }
  222.             }

  223.             if (in4 != null) {
  224.                 try {
  225.                     in4.close();
  226.                 } catch (IOException e) {
  227.                     e.printStackTrace();
  228.                 }
  229.             }
  230.         }
  231.     }

  232. }

阅读(578) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~