无法在java中写入我的属性文件

无法在java中写入我的属性文件,java,Java,我无法写入我的属性文件,因为我不确定为什么没有遇到任何类型的错误或其他问题。我试过调试,但没有找到。这是我的PropertiesUtil类 public class PropertyUtil { public static PropertyUtil prop; private Properties properties; public PropertyUtil() { properties = new Properties(); }

我无法写入我的属性文件,因为我不确定为什么没有遇到任何类型的错误或其他问题。我试过调试,但没有找到。这是我的
PropertiesUtil

public class PropertyUtil {

    public static PropertyUtil prop;
    private Properties properties;

    public PropertyUtil() {
        properties = new Properties();
    }

    public static synchronized PropertyUtil getInstance() {
        if (prop == null) {
            prop = new PropertyUtil();
        }
        return prop;
    }

    public void load(String fileName) throws IOException {
        InputStream input = null;
        input = getClass().getClassLoader().getResourceAsStream(fileName);
        properties.load(input);
    }

    public void load(File file) throws IOException {
        InputStream input = new FileInputStream(file);
        properties.load(input);
    }

    public void clear() {
        properties.clear();
    }

    public String getValue(String key) {
        return properties.getProperty(key).trim();
    }

    public void setValue(String key, String value) {
        properties.setProperty(key, value);
    }

    public HashMap<String, String> propertiesMap() {
        HashMap<String, String> map = new HashMap<String, String>((Map) properties);
        return map;
    }

    public void propertytonull() {
        prop = null;
    }
}
公共类属性直到{
公共静态属性直到道具;
私人物业;;
公共财产直到(){
属性=新属性();
}
公共静态同步属性直到getInstance(){
if(prop==null){
prop=新属性直到();
}
返回道具;
}
公共无效加载(字符串文件名)引发IOException{
InputStream输入=null;
输入=getClass().getClassLoader().getResourceAsStream(文件名);
属性。加载(输入);
}
公共无效加载(文件)引发IOException{
InputStream输入=新文件InputStream(文件);
属性。加载(输入);
}
公共空间清除(){
properties.clear();
}
公共字符串getValue(字符串键){
返回properties.getProperty(key.trim();
}
public void setValue(字符串键、字符串值){
属性。设置属性(键、值);
}
公共HashMap属性Map(){
HashMap map=新的HashMap((map)属性);
返回图;
}
public void propertytonull(){
prop=null;
}
}
这就是我在课堂上写属性文件的方法

prop.setValue(“RequestId”,res_RequestId);
属性设置值(“Id”,res_Id)

RequestId和Id

.properties文件中已存在is密钥


不是该文件中我的问题的答案,将从属性文件中删除所有数据并保存传递的内容。我的问题是验证特定的密钥

这是写入属性文件的方式。下面也是为从属性文件读取而编写的代码

@Test  
public void momSave() {
        FileReader reader = null;
        try {
            reader = new FileReader(filepath/name.properties);
            Properties p = new Properties();
            p.load(reader);
            p.getProperty("RequestId"); // to read from the property file
            p.setProperty("RequestId","53403"); // to read into a property file
properties.store(filepath/name.properties),
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

经过测试并成功运行良好

这是写入属性文件的方式。下面也是为从属性文件读取而编写的代码

@Test  
public void momSave() {
        FileReader reader = null;
        try {
            reader = new FileReader(filepath/name.properties);
            Properties p = new Properties();
            p.load(reader);
            p.getProperty("RequestId"); // to read from the property file
            p.setProperty("RequestId","53403"); // to read into a property file
properties.store(filepath/name.properties),
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

经过测试并成功运行良好

setProperty仅设置程序中的值。您需要保存文件,然后我还看到您正在尝试实现Singleton模式,在这种模式中,您的
PropertyUtil
构造函数应该设置为
private
,这样就不能实例化
PropertyUtil
的其他实例,并且只能通过
getInstance
访问
PropertyUtil
的一个实例;PropertyUtil.getInstance().setValue(“Id”,res_Id);还是一样。你读了链接的答案了吗?是的,但那也没用。你能检查一下我的singleton类并告诉我这里出了什么问题吗?setProperty只设置程序中的值。您需要保存文件,然后我还看到您正在尝试实现Singleton模式,在这种模式中,您的
PropertyUtil
构造函数应该设置为
private
,这样就不能实例化
PropertyUtil
的其他实例,并且只能通过
getInstance
访问
PropertyUtil
的一个实例;PropertyUtil.getInstance().setValue(“Id”,res_Id);还是一样。你读了链接的答案了吗?是的,但那也没用。你能检查一下我的singleton类并告诉我这里出了什么问题吗?这不是将更改的属性集保存到文件中。需要调用属性提供的
store()
方法之一。是的,我已经更新了我的代码。谢谢,我刚刚错过了复制。这不是将更改的属性集保存到文件。需要调用属性提供的
store()
方法之一。是的,我已经更新了我的代码。谢谢,我只是错过了复印。