2014-07-14 32 views
3

,所以我又回到到4年後編寫Java所以請原諒所有「菜鳥」的錯誤裏面。的讀/寫屬性文件的jar文件

我需要有一個屬性文件,我可以存儲一些簡單的數據,我的應用程序。 APP數據本身不會駐留在這裏,但我會存儲信息,如文件路徑上次使用的數據存儲,其他設置等

我設法連接到其存在同一個包裏面的屬性文件作爲類文件試圖連接到它,我可以讀取文件,但我有麻煩回寫到文件。我非常確定我的代碼能夠工作(至少它不會引發任何錯誤),但是在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."); 

} 

正如我上面提到的,一切都運行沒有任何錯誤,我可以玩弄嘗試添加現有的密鑰,並將其拋出預期的錯誤。但是,當試圖添加新的鍵/值對時,它不會顯示在屬性文件後綴中。爲什麼?

回答

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

+0

感謝moskito-x,我想通過這一切,我發現偏好是我正在尋找和需要使用的。我很欣賞幫助,雖然 – BenW301

+0

@ BenW301:我很高興你找到了你的路。 :-)。不用謝。 –

3

你不應該試圖寫該JAR文件的內部存在「文件」。實際上,從技術上講,jar文件不保存文件,而是擁有「資源」,實際上它們是隻讀的。如果您需要讀取和寫入屬性文件,它應該在jar之外。

+0

感謝您的快速反應! 所以,「最佳實踐」明智的是,什麼是最好的方式來存儲「歷史」類型的項目和設置,以便用戶不必每次啓動應用程序時重新連接所有數據和首選項文件?因此,例如,我應該將文件路徑存儲到主屬性文件中?如果用戶的文件路徑沒有以「標準」方式設置,硬編碼似乎會導致問題 – BenW301

+0

@ BenW301:您的代碼是否寫出了在屬性文件中創建文件以外的文件?它看起來應該是這樣做的。 –

+2

@ BenW301有很多選項可供你選擇,檢查[this](http://stackoverflow.com/questions/19556932/how-to-save-the-state-of-my-minesweeper-game-and-然後加載它/ 19557052#19557052)出了一些簡短的解釋。通常情況下,您想要將這些設置存儲在'user.home'目錄中,確切位置將取決於平臺,例如,在linux下它可能是'{user.home} /。YouAppName/...' ,在Windows下它可能是'{user.home}/AppData/Roaming/YouAppName/...' – MadProgrammer

1

挑戰: 閱讀JAR文件的屬性文件位置 讀取屬性文件 寫變量系統變量

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