java中属性文件编写_java – 读取/写入jar文件中的属性文件

所以我在4年后重新开始编写 Java,所以请原谅任何“菜鸟”错误.

我需要一个属性文件,我可以为我的应用程序存储一些简单的数据.应用程序数据本身不会驻留在此处,但我将存储信息,例如上次使用的数据存储的文件路径,其他设置等.

我设法连接到属性文件,该文件存在于与尝试连接到它的类文件相同的包中,我可以读取该文件,但我无法写回文件.我很确定我的代码是有效的(至少它没有丢失任何错误)但是在Netbeans中运行应用程序后,更改并未反映在文件本身中.

在上面的图像中,您可以看到有问题的mainProperties.properties文件以及尝试调用它的类(prefManagement.java).所以考虑到这一点,我的代码是加载文件:

Properties mainFile = new Properties();

try {

mainFile.load(prefManagement.class.getClass().getResourceAsStream("/numberAdditionUI/mainProperties.properties"));

} catch (IOException a) {

System.out.println("Couldn't find/load file!");

}

这工作,我可以检查并确认一个现有的密钥(defaultXMLPath).

我要添加到此文件的代码是:

String confirmKey = "defaultXMLPath2";

String propKey = mainFile.getProperty(confirmKey);

if (propKey == null) {

// Key is not present so enter the key into the properties file

mainFile.setProperty(confirmKey, "testtest");

try{

FileOutputStream fos = new FileOutputStream("mainProperties.properties");

mainFile.store(fos, "testtest3");

fos.flush();

}catch(FileNotFoundException e ){

System.out.println("Couldn't find/load file3!");

}catch(IOException b){

System.out.println("Couldn't find/load file4!");

}

} else {

// Throw error saying key already exists

System.out.println("Key " + confirmKey + " already exists.");

}

正如我上面提到的,一切都运行没有任何错误,我可以尝试添加现有的密钥,它会引发预期的错误.但是当尝试添加新的键/值对时,它不会显示在属性文件中.为什么?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值