Java Properties

读取/写入:

Properties prop = new Properties();
String savePath = ResourceUtils.getURL("csmsystem/src/main/resources/sysconf").getPath();
String systemConf = savePath+"systemBackup_config.properties";
FileInputStream fis = new FileInputStream(systemConf);
//读取键值对
prop.load(fis);
prop.put("backup_dir",backupSet.getContents());
OutputStream os = new FileOutputStream(systemConf);
//持久化到属性文件中
prop.store(os,"Update backup_dir value");
if(null!=fis){
   fis.close();
 }
 if(null!=os){
    os.close();
 }

 

load源码:就是把文件格式化成map

private void load0 (LineReader lr) throws IOException {
        char[] convtBuf = new char[1024];
        int limit;
        int keyLen;
        int valueStart;
        char c;
        boolean hasSep;
        boolean precedingBackslash;
 
        while ((limit = lr.readLine()) >= 0) {
            c = 0;
            keyLen = 0;
            valueStart = limit;
            hasSep = false;
 
            //System.out.println("line=<" + new String(lineBuf, 0, limit) + ">");
            precedingBackslash = false;
            while (keyLen < limit) {
                c = lr.lineBuf[keyLen];
                //need check if escaped.
                if ((c == '=' ||  c == ':') && !precedingBackslash) {
                    valueStart = keyLen + 1;
                    hasSep = true;
                    break;
                } else if ((c == ' ' || c == '\t' ||  c == '\f') && !precedingBackslash) {
                    valueStart = keyLen + 1;
                    break;
                }
                if (c == '\\') {
                    precedingBackslash = !precedingBackslash;
                } else {
                    precedingBackslash = false;
                }
                keyLen++;
            }
            while (valueStart < limit) {
                c = lr.lineBuf[valueStart];
                if (c != ' ' && c != '\t' &&  c != '\f') {
                    if (!hasSep && (c == '=' ||  c == ':')) {
                        hasSep = true;
                    } else {
                        break;
                    }
                }
                valueStart++;
            }
            String key = loadConvert(lr.lineBuf, 0, keyLen, convtBuf);
            String value = loadConvert(lr.lineBuf, valueStart, limit - valueStart, convtBuf);
            put(key, value);
        }
    }
}

你可能感兴趣的:(Java Properties)