我已经设置了一个处理这样的配置文件的类:
public class ConfigHandler{
private static ConfigHandler singleton;
//filepath working...
private String filePath = "./com.triemond.communicate.config/config.cfg";
private Properties configFile;
public ConfigHandler(Communicator communicator){
configFile = new Properties();
InputStream stream = this.getClass().getClassLoader().getResourceAsStream(filePath);
try {
configFile.load(stream);
stream.close();
}catch(Exception e){
e.printStackTrace();
}
}
public void save() throws FileNotFoundException, IOException, URISyntaxException{
URL resourceUrl = getClass().getResource(filePath);
File file = new File(resourceUrl.toURI());
OutputStream stream = new FileOutputStream(file);
configFile.store(stream, null);
stream.close();
}
public String getProperty(String key){
return this.configFile.getProperty(key);
}
public void setProperty(String key, String value) {
configFile.setProperty(key, value);
}
我能够很好地构造这个类(这意味着可以从文件中读取属性。)但是我不能使用相同的路径写入该文件。我得到一个FileNotFoundException。
每当我更改文件的路径时,我都会得到:
- [Load] An error occured while loading the config file
作为一个错误,这意味着catch捕获了一些异常,这就是stacktrace:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at com.triemond.communicate.handlers.ConfigHandler.save(ConfigHandler.java:36)
at com.triemond.communicate.handlers.Communicator.saveProperties(Communicator.java:292)
at com.triemond.communicate.handlers.Communicator.quit(Communicator.java:155)
at com.triemond.communicate.listeners.MenuListener.close(MenuListener.java:129)
at com.triemond.communicate.listeners.MenuListener.actionPerformed(MenuListener.java:42)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6533)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
at java.awt.Component.processEvent(Component.java:6298)
at java.awt.Container.processEvent(Container.java:2237)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2295)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4889)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4526)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4467)
at java.awt.Container.dispatchEventImpl(Container.java:2281)
at java.awt.Window.dispatchEventImpl(Window.java:2746)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:709)
at java.awt.EventQueue$3.run(EventQueue.java:703)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
at java.awt.EventQueue$4.run(EventQueue.java:731)
at java.awt.EventQueue$4.run(EventQueue.java:729)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
为什么我可以从文件中读取但不能写入文件?
答案 0 :(得分:2)
您似乎从类路径加载资源:
this.getClass().getClassLoader().getResourceAsStream(filePath);
但是尝试根据该资源的URL写入文件:
URL resourceUrl = getClass().getResource(filePath);
File file = new File(resourceUrl.toURI());
加载可能有效,因为您的类路径上可能有相应的资源。在某些JAR中打包的资源可能非常好。但它不是文件系统中的文件。
您从getClass().getResource(filePath)
获得的网址可能最终会成为jar:file:/.../some.jar!/com.triemond.communicate.config/config.cfg
。您尝试将其作为文件写入 - 这可能会产生FileNotFoundException
。