zoukankan      html  css  js  c++  java
  • 读写properties文件

    1. 读properties文件

          Properties props = new Properties();
            try {
                InputStream in = new FileInputStream("dataBase.properties");
                props.load(in);
                port = props.getProperty("port");
                ip = props.getProperty("ip");
                baseName = props.getProperty("baseName");
                userName = props.getProperty("userName");
                userPwd = props.getProperty("userPwd");
                conn = connectDataBase();
            } catch (Exception e) {
                logger.error(e.getMessage());
                e.printStackTrace();
            }

    2. 写properties文件

    import java.io.FileInputStream;   
    import java.io.FileOutputStream;   
    import java.util.Properties;   
      
    public class PropertyEditor {   
        public static void main(String[] args) throws Exception {   
            Properties prop = new Properties();// 属性集合对象   
            FileInputStream fis = new FileInputStream("prop.properties");// 属性文件输入流   
            prop.load(fis);// 将属性文件流装载到Properties对象中   
            fis.close();// 关闭流   
      
            // 获取属性值,sitename已在文件中定义   
            System.out.println("获取属性值:sitename=" + prop.getProperty("sitename"));   
            // 获取属性值,country未在文件中定义,将在此程序中返回一个默认值,但并不修改属性文件   
            System.out.println("获取属性值:country=" + prop.getProperty("country", "中国"));   
      
            // 修改sitename的属性值   
            prop.setProperty("sitename", "Boxcode");   
            // 添加一个新的属性studio   
            prop.setProperty("studio", "Boxcode Studio");   
            // 文件输出流   
            FileOutputStream fos = new FileOutputStream("prop.properties");   
            // 将Properties集合保存到流中   
            prop.store(fos, "Copyright (c) Boxcode Studio");   
            fos.close();// 关闭流   
        }   
    }  
  • 相关阅读:
    hdu 1269 迷宫城堡 (并查集)
    hdu 1272 小希的迷宫 (深搜)
    hdu 1026 Ignatius and the Princess I (深搜)
    hdu 1099 Lottery
    hdu 1068 Girls and Boys (二分匹配)
    几个基础数位DP(hdu 2089,hdu 3555,uestc 1307 windy 数)
    hdu 1072 Nightmare (广搜)
    hdu 1398 Square Coins (母函数)
    hdu 1253 胜利大逃亡 (深搜)
    hdu 1115 Lifting the Stone (求重心)
  • 原文地址:https://www.cnblogs.com/sandyflower/p/3629185.html
Copyright © 2011-2022 走看看