簡體   English   中英

當我嘗試更新屬性文件時,屬性文件中的其他值正在更改

[英]When i try to update properties file, other values in properties file is getting changed

我更新了屬性文件,然后看到屬性文件中的其他值也被更改。 我想從Servlet獲取表單值並對其進行更新

properties.java

  static Properties prop =new Properties();
static String PROPERTY_FILENAME = "src/server_url.properties";

public static void main(String[] args) {
    loadProperty();
    System.out.println(prop.getProperty("DemoApps_DataBase"));
    updateProperty("DemoApps_DataBase", "mysql");
}

public static void loadProperty(){

    InputStream input = null;

    try {

     FileInputStream file= new FileInputStream(PROPERTY_FILENAME);
        // load a properties file
        prop.load(file);

    } catch (IOException ex) {
        ex.printStackTrace();
    } finally {
        if (input != null) {
            try {
                input.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

public static void updateProperty(String name, String value){
    OutputStream output = null;

    try {

        output = new FileOutputStream(PROPERTY_FILENAME);

        // set the properties value
        prop.setProperty("DemoApps_DataBase", "mysql");
        System.out.println(prop.getProperty("DemoApps_DataBase"));

        // save properties to project root folder
        prop.store(output, null);

    } catch (IOException io) {
        io.printStackTrace();
    } finally {
        if (output != null) {
            try {
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}

更新之前的屬性文件:

    apps=DemoApps,RelDashBoard
  DemoApps_Links=https://mobileap.com:8090,https://mobileapps.lntinfotech.com:8091
  DemoApps_DataBase=Oracle

更新后的屬性文件:

#Tue Mar 03 14:23:45 IST 2015
 DemoApps_DataBase=MySQL,
 apps=DemoApps,RelDashBoard
    Demo_Links=https\://mobileap.com\:8090,https://mobileapps.lntinfotech.com:8091

:是屬性文件的特殊字符。 因此,它將通過Property類的save方法進行轉義。 如果您讀取文件,轉義符將被刪除。

這里相關代碼:

/*
 * Converts unicodes to encoded \uxxxx and escapes
 * special characters with a preceding slash
 */
private String saveConvert(String theString,
                           boolean escapeSpace,
                           boolean escapeUnicode) {
    int len = theString.length();
    int bufLen = len * 2;
    if (bufLen < 0) {
        bufLen = Integer.MAX_VALUE;
    }
    StringBuffer outBuffer = new StringBuffer(bufLen);

    for(int x=0; x<len; x++) {
        char aChar = theString.charAt(x);
        // Handle common case first, selecting largest block that
        // avoids the specials below
        if ((aChar > 61) && (aChar < 127)) {
            if (aChar == '\\') {
                outBuffer.append('\\'); outBuffer.append('\\');
                continue;
            }
            outBuffer.append(aChar);
            continue;
        }
        switch(aChar) {
            case ' ':
                if (x == 0 || escapeSpace)
                    outBuffer.append('\\');
                outBuffer.append(' ');
                break;
            case '\t':outBuffer.append('\\'); outBuffer.append('t');
                      break;
            case '\n':outBuffer.append('\\'); outBuffer.append('n');
                      break;
            case '\r':outBuffer.append('\\'); outBuffer.append('r');
                      break;
            case '\f':outBuffer.append('\\'); outBuffer.append('f');
                      break;
            case '=': // Fall through
            case ':': // Fall through
            case '#': // Fall through
            case '!':
                outBuffer.append('\\'); outBuffer.append(aChar);
                break;
            default:
                if (((aChar < 0x0020) || (aChar > 0x007e)) & escapeUnicode ) {
                    outBuffer.append('\\');
                    outBuffer.append('u');
                    outBuffer.append(toHex((aChar >> 12) & 0xF));
                    outBuffer.append(toHex((aChar >>  8) & 0xF));
                    outBuffer.append(toHex((aChar >>  4) & 0xF));
                    outBuffer.append(toHex( aChar        & 0xF));
                } else {
                    outBuffer.append(aChar);
                }
        }
    }
    return outBuffer.toString();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM