我有一個小問題,應該保存包含在JTextArea中的文本。當我從默認的構造函數調用這個方法時,這並不是一個問題,並且工作。如果我叫從一個JFileChooser我得到以下異常的動作監聽器方法(是如何產生的文件,但也異常是越來越拋出):java保存文件堆棧跟蹤
Exception in thread "AWT-EventQueue-0" java.lang.ArrayIndexOutOfBoundsException: 6
at text_editor.Editor.saveFile(Editor.java:186)
at text_editor.Editor.access$0(Editor.java:179)
at text_editor.Editor$SaveActionListener.actionPerformed(Editor.java:155)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2028)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2351)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
at javax.swing.AbstractButton.doClick(AbstractButton.java:389)
at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:809)
at com.apple.laf.AquaMenuItemUI.doClick(AquaMenuItemUI.java:137)
at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:850)
at java.awt.Component.processMouseEvent(Component.java:6373)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3267)
at java.awt.Component.processEvent(Component.java:6138)
at java.awt.Container.processEvent(Container.java:2085)
at java.awt.Component.dispatchEventImpl(Component.java:4735)
at java.awt.Container.dispatchEventImpl(Container.java:2143)
at java.awt.Component.dispatchEvent(Component.java:4565)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4621)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4282)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4212)
at java.awt.Container.dispatchEventImpl(Container.java:2129)
at java.awt.Window.dispatchEventImpl(Window.java:2478)
at java.awt.Component.dispatchEvent(Component.java:4565)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:679)
at java.awt.EventQueue.access$000(EventQueue.java:85)
at java.awt.EventQueue$1.run(EventQueue.java:638)
at java.awt.EventQueue$1.run(EventQueue.java:636)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:652)
at java.awt.EventQueue$2.run(EventQueue.java:650)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:649)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:296)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:196)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:188)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)
這是我的拯救方法:
private void saveFile(String path) throws IOException, BadLocationException{
BufferedWriter buffer = null;
try {
buffer = new BufferedWriter(new FileWriter(new File(path)));
String lines[] = textfield.getText().split("\\n");
for (int i = 0; i < this.textfield.getLineCount(); i++) {
buffer.write(lines[i] + System.getProperty("line.separator"));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (buffer != null) {
buffer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
JOptionPane.showMessageDialog(frame, "File saved","Information", JOptionPane.INFORMATION_MESSAGE);
}
這個JFielChooser動作偵聽器:
private class SaveActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
try {
final JFileChooser fc = new JFileChooser();
int returnVal = fc.showSaveDialog(frame);
if (returnVal == JFileChooser.APPROVE_OPTION) {
File file = fc.getSelectedFile();
try {
saveFile(file.getAbsolutePath());
} catch (BadLocationException e1) {
e1.printStackTrace();
}
}
} catch (IOException e1) {
System.out.println("ERROR: Could not save ArrayList to the FileSystem");
JOptionPane.showMessageDialog(null, "Could not save the selected file","Error", JOptionPane.ERROR_MESSAGE);
}
}
}
不知道原因,但可能只是做一個.replaceAll(「\\ n」,System.getProperty(「line.separator」))可以使用,所以人們不需要使用容易出錯的循環? – eis 2012-03-24 10:55:57