2014-02-05 179 views
0

我想寫入屬性文件。 我也從屬性文件中讀取。看看這裏Path for a properties file in maven從maven項目寫入屬性文件

public void setProperty(String coll, String bez) throws URISyntaxException { 
    File file =null; 
    Properties properties = getProperties();  
    URL url = Thread.currentThread().getContextClassLoader().getResource("/jsfatwork/messages/test.properties"); 
    URL url2 = PageServiceBean.class.getResource("/jsfatwork/messages/test.properties");        
    try {  
     file = new File(url.toURI().getPath());    
     properties.setProperty(bez, coll); 
     System.out.println("Ich habe einen Wert gesetzt"); 
     properties.store(new FileOutputStream(file), null);  
     System.out.println("Die Datei wurde gespeichert"); 
    } catch (IOException ex) { 
     Logger.getLogger(PageServiceBean.class.getName()).log(Level.SEVERE, null, ex);    
    } 
} 

控制檯輸出爲:

Ich habe einen Wert gesetzt 
Die Datei wurde gespeichert 

所以我覺得它的工作原理,但< 5秒鐘後,我得到一個execption:

Warnung: Cannot serialize session attribute pageServiceBean for session 143CD5DE1C101E6A30515C585C32C02B 
    java.io.NotSerializableException: com.mongodb.DBApiLayer 
     at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1183) 
     at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1547) 
     at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1508) 
     at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1431) 
     at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1177) 
     at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347) 
     at org.apache.catalina.session.StandardSession.writeObject(StandardSession.java:1671) 
     at org.apache.catalina.session.StandardSession.writeObjectData(StandardSession.java:1077) 
     at org.apache.catalina.session.StandardManager.doUnload(StandardManager.java:432) 
     at org.apache.catalina.session.StandardManager.unload(StandardManager.java:353) 
     at org.apache.catalina.session.StandardManager.stopInternal(StandardManager.java:518) 
     at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232) 
     at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5622) 
     at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232) 
     at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3947) 
     at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:426) 
     at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1345) 
     at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1530) 
     at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540) 
     at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540) 
     at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1519) 
     at java.lang.Thread.run(Thread.java:744) 
... 
Schwerwiegend: IOException while loading persisted sessions: java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: com.mongodb.DBApiLayer 
java.io.WriteAbortedException: writing aborted; java.io.NotSerializableException: com.mongodb.DBApiLayer 
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1354) 
    at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1990) 
    at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1915) 
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1798) 
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1350) 
    at java.io.ObjectInputStream.readObject(ObjectInputStream.java:370) 
    at org.apache.catalina.session.StandardSession.readObject(StandardSession.java:1595) 
    at org.apache.catalina.session.StandardSession.readObjectData(StandardSession.java:1060) 
    at org.apache.catalina.session.StandardManager.doLoad(StandardManager.java:284) 
    at org.apache.catalina.session.StandardManager.load(StandardManager.java:204) 
    at org.apache.catalina.session.StandardManager.startInternal(StandardManager.java:491) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5443) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
    at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3954) 
    at org.apache.catalina.loader.WebappLoader.backgroundProcess(WebappLoader.java:426) 
    at org.apache.catalina.core.ContainerBase.backgroundProcess(ContainerBase.java:1345) 
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1530) 
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540) 
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.processChildren(ContainerBase.java:1540) 
    at org.apache.catalina.core.ContainerBase$ContainerBackgroundProcessor.run(ContainerBase.java:1519) 
    at java.lang.Thread.run(Thread.java:744) 
Caused by: java.io.NotSerializableException: com.mongodb.DBApiLayer 
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1183) 
    at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1547) 
    at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1508) 
    at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1431) 
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1177) 
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:347) 
    at org.apache.catalina.session.StandardSession.writeObject(StandardSession.java:1671) 
    at org.apache.catalina.session.StandardSession.writeObjectData(StandardSession.java:1077) 
    at org.apache.catalina.session.StandardManager.doUnload(StandardManager.java:432) 
    at org.apache.catalina.session.StandardManager.unload(StandardManager.java:353) 
    at org.apache.catalina.session.StandardManager.stopInternal(StandardManager.java:518) 
    at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232) 
    at org.apache.catalina.core.StandardContext.stopInternal(StandardContext.java:5622) 
    at org.apache.catalina.util.LifecycleBase.stop(LifecycleBase.java:232) 
    at org.apache.catalina.core.StandardContext.reload(StandardContext.java:3947) 
    ... 7 more 

我使用MongoDB的作爲數據庫。但在這種情況下,我寫入屬性文件而不寫入數據庫。所以我不知道爲什麼我會得到一個java.io.NotSerializableException: com.mongodb.DBApiLayer錯誤。

而且,在異常之後打開文件後,文件中的屬性不存在。

有什麼想法?

回答

0

我解決了這個問題。 我無法寫入類路徑中的propertie文件,因爲它在類路徑中是它在服務器上的war文件的一部分。而且在我寫入數據之後不再一致。所以你得到錯誤。 解決的辦法是將properties文件放在類路徑中。 我用FileOutputStream寫入我的propertie文件並使用eclipse HOME PATH作爲路徑。 這是一個很好的解決方案,或者我應該使用不同的方式寫在屬性文件中?