我一直在编写文本编辑器,我创建了一个菜单,可以保存文件。我能够编写保存文件所需的代码,但是如何检查该特定文件是否已保存?如果已保存,则只保存对该文件的任何更改,但如果尚未保存,请使用新名称保存该文件。任何帮助都将非常感谢,因为我已经阅读了java api以及已经发布在此处的类似问题,似乎对我没有任何帮助。这就是我所拥有的:
JFileChooser saveAsChooser = new JFileChooser();
saveAsItem.addActionListener(event -> {
int saveResult = saveAsChooser.showSaveDialog(this);
File myFile = saveAsChooser.getSelectedFile();
String fileName = myFile.getName().toString();
setMyTitle("Swing Text Editor [" + fileName + "]");
if(saveResult == JFileChooser.APPROVE_OPTION)
{
try {
PrintWriter file = new PrintWriter(new File(myFile.getName()));
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
});
JFileChooser saveChooser = new JFileChooser();
saveItem.addActionListener(event -> {
int saveResult = saveChooser.showSaveDialog(this);
File myFile = saveAsChooser.getSelectedFile();
File myFileName = new File(myFile.getName());
String fileName = myFile.getName().toString();
setMyTitle("Swing Text Editor [" + fileName + "]");
if(saveResult == JFileChooser.APPROVE_OPTION)
{
if(myFileName.exists() == false)
{
try {
PrintWriter file = new PrintWriter(new File(myFile.getName()));
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
else if(saveResult == JFileChooser.APPROVE_OPTION)
{
if(myFileName.exists() == true)
{
try {
PrintWriter file = new PrintWriter(myFile.getName());
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
这就是崩溃时所说的内容:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at MyFrame.lambda$new$3(Editor.java:146)
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.AbstractButton.doClick(AbstractButton.java:376)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
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:2236)
at java.awt.Component.dispatchEventImpl(Component.java:4889)
at java.awt.Container.dispatchEventImpl(Container.java:2294)
at java.awt.Component.dispatchEvent(Component.java:4711)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4525)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
at java.awt.Container.dispatchEventImpl(Container.java:2280)
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:76)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:86)
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:76)
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)