读取/写入jar文件中的属性文件

时间:2014-07-14 01:48:26

标签: java netbeans windows-8 properties-file

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

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

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

enter image description here

在上图中,您可以看到有问题的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.");

}

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

3 个答案:

答案 0 :(得分:3)

您不应该尝试写入"文件"存在于jar文件中。实际上,从技术上讲,jar文件不会保存文件,而是保存资源"并且出于实际目的,它们是只读的。如果您需要读取和写入属性文件,它应该在jar之外。

答案 1 :(得分:2)

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

运行部分代码后,您会发现本地已创建文件mainProperties.properties

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

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

  • 阅读资源mainProperties.properties
  • 的完整内容
  • 将所有必要的属性写入local文件mainAppProp.properties

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

切换文件是否存在于本地文件中,如果没有创建文件mainAppProp.properties并将所有属性写入其中。

  • 测试文件mainAppProp.properties是否存在于本地。
  • 将属性读入新的“probs”变量。
  • 从现在开始仅使用此文件。

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

测试就像

    [...]
    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

文件mainAppProp.properties

的内容

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

答案 2 :(得分:1)

挑战: 读取jar文件中的Property文件位置 阅读Property文件 将变量写为系统变量

public static void loadJarCongFile(Class Utilclass )
    {
       try{         
             String path= Utilclass.getResource("").getPath();
             path=path.substring(6,path.length()-1);
             path=path.split("!")[0];
             System.out.println(path);
             JarFile jarFile = new JarFile(path);

               final Enumeration<JarEntry> entries = jarFile.entries();
               while (entries.hasMoreElements()) {
                   final JarEntry entry = entries.nextElement();
                   if (entry.getName().contains(".properties")) {
                       System.out.println("Jar File Property File: " + entry.getName());
                       JarEntry fileEntry = jarFile.getJarEntry(entry.getName());
                       InputStream input = jarFile.getInputStream(fileEntry);
                       setSystemvariable(input);      
                       InputStreamReader isr = new InputStreamReader(input); 
                       BufferedReader reader = new BufferedReader(isr);
                       String line;

                       while ((line = reader.readLine()) != null) {
                           System.out.println("Jar file"+line);
                       }
                       reader.close();
                   }
               }
       }
       catch (Exception e)
       {
          System.out.println("Jar file reading Error");
       }
    }
    public static void setSystemvariable(InputStream input)
    {
    Properties tmp1 = new Properties();
       try {
             tmp1.load(input);

       for (Object element : tmp1.keySet()) {
             System.setProperty(element.toString().trim(),
                           tmp1.getProperty(element.toString().trim()).trim());
             }      
       } catch (IOException e) {
             System.out.println("setSystemvariable method failure");
       }
    }
相关问题