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

查看:120
本文介绍了在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.

After you run your part of code, there you will find that a file mainProperties.properties has been created locally.

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是否在本地存在.
  • 将属性读取到新的概率"变量中.
  • 从现在开始仅使用此文件.
  • Test if file mainAppProp.properties exists locally.
  • Read the properties into a new "probs" variable.
  • Use only this file from now on.

在任何情况下,您都不能将属性写回到.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

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

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

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