在java Web应用程序中写入.properties文件

时间:2014-04-17 18:49:10

标签: java

我试图将一个键值对写入java .My函数中的chat.properties文件,这样做是这样的:

public void WritePropertiesFile() throws FileNotFoundException, IOException {
    File file = 
        new File("C:\\Users\\admin\\Desktop\\SharedCrpto1\\web\\chat.properties");
    Properties configProperty = new Properties();
    InputStream in = new FileInputStream(file);
    configProperty.load(in);    
    configProperty.setProperty("newKey", "newValue");
    in.close();
    OutputStream outt = new FileOutputStream(file); 
    configProperty.store(outt, "my data");
    outt.close();
}

但它不起作用,数据没有输入文件。请帮助解决问题。

1 个答案:

答案 0 :(得分:0)

试试这段代码:

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

public class MakeEntryInPropertyFile {
  public static void main(String[] args) {

    Properties prop = new Properties();
    OutputStream output = null;

    try {

        output = new FileOutputStream("C:\\Users\\admin\\Desktop\\SharedCrpto1\\web\\chat.properties");
        prop.setProperty("newKey", "newValue");
        prop.store(output, null);
    } catch (IOException io) {
        io.printStackTrace();
    } finally {
        if (output != null) {
            try {
                output.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
  }
}