0

我正在从 UI 更改属性文件中的 2 个字段,我认为一切正常,但现在我遇到问题,当我更改 2 个字段并单击提交时,整个文件混乱,顶部缺少一些行。这是我实际的 userdata.properties 文件。

!**
! * 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

但是在更改代理=192.168.10.73 和端口=8073 之后,这些值正在更改,但文件看起来像这样

#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

这是我用于此功能的控制器类,我无法找到我的错误,请帮助我纠正这个问题。

@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";

    }

}
4

1 回答 1

0

当您使用 Java 属性时,它将跳过代码的注释部分。因此,当您将其写回时,注释部分会丢失。

Java 属性旨在仅从属性文件中提取键值对。所有评论仅供人类阅读。

所以你的代码中的属性对象没有关于注释代码的任何信息。因此,当您执行 properties.store() 时,它只保存键值对而不是以前的注释。

我不建议以编程方式更改属性文件的内容。但如果你这样做,你可以使用 FileWriter 并像普通文本文件一样写入文件。您可以在此处查看示例。

于 2012-06-19T05:58:09.940 回答