博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

配置文件

Posted on 2017-08-15 17:12  亡者归来19  阅读(86)  评论(0)    收藏  举报
//1.把配置写入一个配置文件中
            Properties p = new Properties();
            p.setProperty("key",    "value");
// 1.1 例子:
    -->        p.setProperty("user", "scott");
    -->        p.setProperty("password", "tiger");
            //此配置写入文件中
            File f = new File("D:","oracle.xml"); 
      //写的文件路径
FileOutputStream fos
= new FileOutputStream(f);
      //
FileOutputStream 是把文件的内容提取出来(往外送)
  p.storeToXML(fos, "oracleConnection"); --写的是文件格式"oracleConnection" --可改变
  fos.flush(); --刷新
   fos.close(); --关闭
//2.从配置文件把配置读出来
            Properties p = new Properties();
            File f = new File("D:","oracle.xml");
            FileInputStream fis = new FileInputStream(f);
      //
FileInputStream 是把文件内容写入
         p.loadFromXML(fis); String url = p.getProperty("key"); 
//例子: String user = p.getProperty("user");
String psd
= p.getProperty("password");
System.out.println(key值);
System.out.println(user);
System.out.println(psd);
fis.close()