Data is changing in properties file after changing 2 rows from UI

I am changing 2 fields in properties file from UI,i thought every thing is working fine,but now i had problem that when i change the 2 fields and click submit ,the whole file is jumbled and at top some lines are missing.this is my actual userdata.properties file.

!**
! * Sahi - Web Automation and Test Tool
! * 
! * Copyright  2006  V Narayan Raman
! *
! * Licensed under the Apache License, Version 2.0 (the "License");
! * you may not use this file except in compliance with the License.
! * You may obtain a copy of the License at
! *
! *    http://www.apache.org/licenses/LICENSE-2.0
! *
! * Unless required by applicable law or agreed to in writing, software
! * distributed under the License is distributed on an "AS IS" BASIS,
! * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
! * See the License for the specific language governing permissions and
! * limitations under the License.
! **
# dirs. Relative paths are relative to userdata dir. Separate directories with semi-colon
scripts.dir=scripts;
# default log directory.
logs.dir=logs
# Directory where auto generated ssl cerificates are stored
certs.dir=certs

# Use external proxy server for http
ext.http.proxy.enable=true
ext.http.proxy.host=192.168.10.63
ext.http.proxy.port=8063
ext.http.proxy.auth.enable=false
ext.http.proxy.auth.name=kamlesh
ext.http.proxy.auth.password=password

# Use external proxy server for https
ext.https.proxy.enable=true
ext.https.proxy.host=
ext.https.proxy.port=
ext.https.proxy.auth.enable=false
ext.https.proxy.auth.name=kamlesh
ext.https.proxy.auth.password=password

# There is only one bypass list for both secure and insecure.
ext.http.both.proxy.bypass_hosts=localhost|127.0.0.1|*.internaldomain.com

But after changing the proxy=192.168.10.73 and port=8073.those values are changing but file looks like this

#hostchanged
#Tue Jun 19 10:53:00 IST 2012
ext.https.proxy.auth.password=password
ext.http.proxy.host=192.168.10.73
ext.https.proxy.auth.name=kamlesh
ext.http.proxy.auth.password=password
ext.http.proxy.port=8073
ext.http.proxy.enable=true
certs.dir=certs
ext.http.proxy.auth.enable=false
ext.https.proxy.host=
ext.http.proxy.auth.name=kamlesh
scripts.dir=scripts;
logs.dir=logs
ext.https.proxy.auth.enable=false
ext.https.proxy.port=
ext.https.proxy.enable=true
ext.http.both.proxy.bypass_hosts=localhost|127.0.0.1|*.internaldomain.com

This my controller class that i am using for this functionality,i am not able to find my mistake,please help me to correct this.

@Controller
public class MobeeProxyChangeController {

    private @Value("${filePath}")String fileDir;

    @RequestMapping("/proxy")
    public String ProxySettings(Model model) throws Exception {

        File f = new File(fileDir);

        Properties properties = new Properties();

        try {
            properties.load(new FileInputStream(f));

            String getHost = properties.getProperty("ext.http.proxy.host");
            String getPort = properties.getProperty("ext.http.proxy.port");

            model.addAttribute("proxyHost", getHost.trim());
            model.addAttribute("proxyPort", getPort.trim());
        } catch (Exception e) {
            e.printStackTrace();
        }

        return "proxyFile";

    }

    @RequestMapping("/saveProxy")
    public String SaveProxy(Model model, HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        File f = new File(fileDir);
        Properties properties = new Properties();

        properties.load(new FileInputStream(f));
        OutputStream outFile = new FileOutputStream(f);

        System.out.println("output file-------------:"+outFile);

        try {
            String httpHost = request.getParameter("proxyHost");
            String httpPort = request.getParameter("proxyPort");

            if (properties.get("ext.http.proxy.host") != null
                    && properties.get("ext.http.proxy.port") != null) {

                properties.setProperty("ext.http.proxy.host", httpHost.trim());
                properties.setProperty("ext.http.proxy.port", httpPort.trim());
                properties.store(outFile, "hostchanged");

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            outFile.close();
        }

        return "redirect:/mainPage";

    }

}

Answers


When you use Java Properties, it will skip the commented part of the code. Hence the commented part is missing when you write it back.

Java Properties is designed to extract only key value pairs from the properties file. All comments are for human readability only.

So properties object in your code does not have any information about the commented code. So when you do properties.store() it saves only the key value pairs and not the previous comments.

I would not recommend to change content of a properties file programatically. But In case you do you can use FileWriter and write to the file like a normal text file. You see examples here.


Need Your Help

SQL statement HAVING MAX(some+thing)=some+thing

sql ms-access group-by aggregate having

I'm having trouble with Microsoft Access 2003, it's complaining about this statement: