2012-03-24 131 views
0

我有一個小問題,應該保存包含在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); 
     } 
    } 

} 
+0

不知道原因,但可能只是做一個.replaceAll(「\\ n」,System.getProperty(「line.separator」))可以使用,所以人們不需要使用容易出錯的循環? – eis 2012-03-24 10:55:57

回答

1

你得到一個ArrayIndexOutOfBoundsException所以我想,當你調用lines[i]它被拋出:

for (int i = 0; i < this.textfield.getLineCount(); i++)

大概應該是:

for (int i = 0; i < lines.length; i++)

+0

該死的。多麼愚蠢的錯誤。感謝您的幫助...我現在正在尋找一小時以後,但沒有找到解決方案。歡呼聲 – dom 2012-03-24 10:56:56

1

您的問題是分裂的分裂換行符( 「\ n」)返回不同的數,然後getTextLines()在你的文本區。 再後來你用一個for循環是這樣的:

for (int i = 0; i < ______this.textfield.getLineCount() ______; i++) { 
      buffer.write(lines[i] + System.getProperty("line.separator")); 
     } 

替換this.textfield.getLineCount()與你的String []線。長度字段 這樣的:

for (int i = 0; i < lines.length; i++) { 
     buffer.write(lines[i] + System.getProperty("line.separator")); 
} 
+0

他速度更快...... – Nightmares 2012-03-24 10:58:02