读取/写入 jar 文件中的属性文件 [英] Reading/Writing to Properties Files inside the jar file

查看:23
本文介绍了读取/写入 jar 文件中的属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在 4 年后重新开始编写 Java,所以请原谅任何新手"错误.

So i am getting back into writing Java after 4 years so please forgive any "rookie" mistakes.

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

I need to have a properties file where i can store some simple data for my application. The app data itself won't reside here but i will be storing info such as the file path to the last used data store, other settings, etc.

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

I managed to connect to the properties file which exists inside the same package as the class file attempting to connect to it and i can read the file but i am having trouble writing back to the file. I am pretty sure that my code works (at least it's not throwing any errors) but the change isn't reflected in the file itself after the app is run in Netbeans.

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

In the above image you can see the mainProperties.properties file in question and the class attempting to call it (prefManagement.java). So with that in mind here is my code to load the file:

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).

This works and i can check and confirm the one existing key (defaultXMLPath).

我添加到这个文件的代码是:

My code to add to this file is:

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.");

}

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

As i mentioned above, everything runs without any errors and i can play around with trying to add the existing key and it throws the expected error. But when trying to add a new key/value pair it doesn't show up in the properties file afterwords. Why?

推荐答案

您的代码将属性写入本地文件 mainProperties.properties.

Your code writes to a local file mainProperties.properties the properties.

运行您的部分代码后,您会发现在本地创建了一个文件 mainProperties.properties.

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

可以命令不要混淆您指定的本地文件到另一个名称的两个文件.例如mainAppProp.properties .

Could order not to confuse the two files you specify the local file to another name. e.g. mainAppProp.properties .

  • 阅读资源mainProperties.properties的完整内容.
  • 将所有必要的属性写入 local 文件 mainAppProp.properties.
  • Read the complete contents of the resource mainProperties.properties.
  • Write all the necessary properties to the local file mainAppProp.properties.
 FileOutputStream fos = new FileOutputStream("mainAppProp.properties");

如果文件存在则切换到本地文件,如果不存在则创建文件mainAppProp.properties并将所有属性写入其中.

switch if file exists to your local file , if not create the file mainAppProp.properties and write all properties to it.

  • 测试文件 mainAppProp.properties 是否在本地存在.
  • 将属性读入新的probs"变量.
  • 从现在开始只使用这个文件.

在任何情况下,您都不能将属性写回到 .jar 文件中.

Under no circumstances you can write the properties back into the .jar file.

测试一下

    [...]
    if (propKey == null) {
    // Key is not present so enter the key into the properties file
    mainFile.setProperty(confirmKey, "testtest");


    [...]
    Reader reader = null;
    try
    {
    reader = new FileReader( "mainAppProp.properties" );
    Properties prop2 = new Properties();
    prop2.load( reader );
    prop2.list( System.out );
    }
    catch ( IOException e )
    {
    e.printStackTrace();
    }
    finally
    {
    if (reader != null) {
    reader.close(); 
    }
    }
    }
    [...]
   }

输出:使用 prop2.list( System.out );

-- 列出属性--
defaultXMLPath2=testtest

-- listing properties --
defaultXMLPath2=testtest

文件内容mainAppProp.properties

#testtest3
#Mon Jul 14 14:33:20 BRT 2014
defaultXMLPath2=testtest

这篇关于读取/写入 jar 文件中的属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆