且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我们可以在运行时更新.properties文件

更新时间:2022-04-12 07:40:49



java中有专用的类来处理属性:java.util.Properties

请查看 javadoc [ ^ ]了解更多信息。

以下是使用示例:



Hi,
There is dedicated class in java to handle properties: java.util.Properties
Look at javadoc[^]for more information.
Here is example of use:

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class PropertiesTest {

  public static void main(String[] args)  {
    Properties props = new Properties();

    String propsFileName = "./src/myconfig.properties";
    try {
      //first load old one:
      FileInputStream configStream = new FileInputStream(propsFileName);
      props.load(configStream);
      configStream.close();

      //modifies existing or adds new property
      props.setProperty("connection", "new connection settings go here");
      props.setProperty("newProperty", "newValue");

      //save modified property file
      FileOutputStream output = new FileOutputStream(propsFileName);
      props.store(output, "This description goes to the header of a file");
      output.close();

    } catch (IOException ex) {
      ex.printStackTrace();
    }
  }
}


HI,

但如果我运行相同的代码在服务器上,它给我一个错误,如没有这样的文件或目录。

but if I run the same code on server it gives me error like No such file or directory.