我有一个jsp文件,我在其中加载属性文件中存在的现有值。当用户编辑现有值并提交表单时,必须使用该值更新属性文件。谁能帮我这个?我只使用java。
答案 0 :(得分:0)
FileInputStream in = new FileInputStream("Example.properties");
Properties props = new Properties();
props.load(in);
现在更新
FileOutputStream outputStream = new FileOutputStream("Example.properties");
props.setProperty("valueTobeUpdate", "new Value");
props.store(outputStream , null);
outputStream .close();
另一种实现相同目的的方法在
中解释http://crunchify.com/java-properties-files-how-to-update-config-properties-file-in-java/
答案 1 :(得分:0)
以下是如何更新属性文件的示例:
public class PropertyManager {
private static Properties prop = new Properties();
private static String PROPERTY_FILENAME = "config.properties";
public static void main(String[] args) {
loadProperty();
System.out.println(prop.get("myProperty"));
updateProperty("myProperty", "aSecondValue");
}
public static void loadProperty(){
InputStream input = null;
try {
input = new FileInputStream(PROPERTY_FILENAME);
// load a properties file
prop.load(input);
} 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(name, value);
// 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();
}
}
}
}
我允许您通过检索它来更改“新属性”。
答案 2 :(得分:0)
PropertiesConfiguration config = new PropertiesConfiguration("/Users/abc/Documents/config.properties");
config.setProperty("Name", "abcd");
config.setProperty("Email", "abcd@gmail.com");
config.setProperty("Phone", "123456");
config.save();