Java:如何在Java中轻松更改配置文件值? [英] Java: How to I change the configuration file value in Java easily?

查看:1330
本文介绍了Java:如何在Java中轻松更改配置文件值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个配置文件,命名为config.txt,看起来像这样。

  IP = 192.168.1.145 
PORT = 10022
URL = http://www.stackoverflow.com

在Java中更改配置文件的一些值,例如端口为10045.我如何轻松实现?

  IP = 192.168。 1.145 
PORT = 10045
URL = http://www.stackoverflow.com



<在我的试验中,我需要写大量的代码来读取每一行,找到PORT,删除原来的10022,然后重写10045.我的代码是虚拟的,很难阅读。在java中有什么方便的方法吗?



非常感谢。

解决方案

p>如果你想要的东西短,你可以使用这个。

  public static void changeProperty(String filename,String key,String value) throws IOException {
属性prop = new Properties();
prop.load(new FileInputStream(filename));
prop.setProperty(key,value);
prop.store(new FileOutputStream(filename),null);
}

不幸的是,它不保留订单或字段或任何注释。 p>

如果您想保留订单,一次读取一行不会太差。






这个未经测试的代码将保留注释,空行和顺序。它不会处理多行值。

  public static void changeProperty(String filename,String key,String value)throws IOException {
final File tmpFile = new File(filename +.tmp);
final File file = new File(filename);
PrintWriter pw = new PrintWriter(tmpFile);
BufferedReader br = new BufferedReader(new FileReader(file));
boolean found = false;
final String toAdd = key +'='+ value;
for(String line;(line = br.readLine())!= null;){
if(line.startsWith(key +'=')){
line = toAdd;
found = true;
}
pw.println(line);
}
if(!found)
pw.println(toAdd);
br.close();
pw.close();
tmpFile.renameTo(file);
}


I have a config file, named config.txt, look like this.

IP=192.168.1.145
PORT=10022
URL=http://www.stackoverflow.com

I wanna change some value of the config file in Java, say the port to 10045. How can I achieve easily?

IP=192.168.1.145
PORT=10045
URL=http://www.stackoverflow.com

In my trial, i need to write lots of code to read every line, to find the PORT, delete the original 10022, and then rewrite 10045. my code is dummy and hard to read. Is there any convenient way in java?

Thanks a lot !

解决方案

If you want something short you can use this.

public static void changeProperty(String filename, String key, String value) throws IOException {
   Properties prop =new Properties();
   prop.load(new FileInputStream(filename));
   prop.setProperty(key, value);
   prop.store(new FileOutputStream(filename),null);
}

Unfortunately it doesn't preserve the order or fields or any comments.

If you want to preserve order, reading a line at a time isn't so bad.


This untested code would keep comments, blank lines and order. It won't handle multi-line values.

public static void changeProperty(String filename, String key, String value) throws IOException {
    final File tmpFile = new File(filename + ".tmp");
    final File file = new File(filename);
    PrintWriter pw = new PrintWriter(tmpFile);
    BufferedReader br = new BufferedReader(new FileReader(file));
    boolean found = false;
    final String toAdd = key + '=' + value;
    for (String line; (line = br.readLine()) != null; ) {
        if (line.startsWith(key + '=')) {
            line = toAdd;
            found = true;
        }
        pw.println(line);
    }
    if (!found)
        pw.println(toAdd);
    br.close();
    pw.close();
    tmpFile.renameTo(file);
}

这篇关于Java:如何在Java中轻松更改配置文件值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆