配置文件properties

web程序时,放在        非web程序,放在src下

扩展名为properties;格式为key=value,key一般使用 . 隔开,value不支持中文;

加载方法   其中配置文件名为db.properties

  1. 直接加载bundle
     1     private static String driver ;
     2     private static String url ;
     3     private static String username ;
     4     private static String password ;
     5     static{
     6         ResourceBundle bundle = ResourceBundle.getBundle("db");
     7         driver = bundle.getString("driver");
     8         url = bundle.getString("url");
     9         username = bundle.getString("username");
    10         password = bundle.getString("password");
    11     }
     
  2. 加载文件流
     1     private static String driver ;
     2     private static String url ;
     3     private static String username ;
     4     private static String password ;
     5     static{
     6         try {
     7         //1.通过当前类获取类的加载器
     8         ClassLoader loader = JDBCUtils_V3.class.getClassLoader();
     9         //2.通过加载器方法获得输入流
    10         InputStream is = loader.getResourceAsStream("db.properties");
    11         //3.创建properties对象
    12         Properties properties = new Properties();
    13         //4.加载输入流        
    14         properties.load(is);        
    15         //5.获取配置文件的信息
    16         driver = properties.getProperty("driver");
    17         url = properties.getProperty("url");
    18         username = properties.getProperty("username");
    19         password = properties.getProperty("password");
    20         } catch (IOException e) {
    21             // TODO Auto-generated catch block
    22             e.printStackTrace();
    23         }
    24     }

猜你喜欢

转载自www.cnblogs.com/vvxl/p/10927120.html