Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

[Java] - JTextArea Problem

Options
  • 05-03-2008 6:34pm
    #1
    Registered Users Posts: 527 ✭✭✭


    Hi all,

    I was writing a GUI in Java and I wanted to have a JTextArea that has multiple lines of text and a JButton that when clicked wrote the contents of the JTextArea to a .txt file.

    Here's my code:
    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
            JFileChooser jfc = new JFileChooser();
            String tStore = jTextArea1.getText();
            int x = jTextArea1.getLineCount();
            System.out.println(x);
    
            if (evt.getSource() == jButton4) {
                int returnVal = jfc.showSaveDialog(HTMLStripperMain.this);
    
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                    File file = jfc.getSelectedFile();
                    String fileName = file.getAbsolutePath();
                    fileName = fileName.concat(".txt");
                    System.out.println("Saving: " + file.getName() + ".");
                    System.out.println(fileName);
                    int totalLines = jTextArea1.getLineCount();
    
                    try {
                        BufferedWriter out = new BufferedWriter(new FileWriter(fileName, true));
                        for (int i = 0; i < totalLines; i++) {
                            try {
                                int start = jTextArea1.getLineStartOffset(i);
                                int end = jTextArea1.getLineEndOffset(i);
                                String line = tStore.substring(start, end);
                                out.write(line);
                                out.newLine();
                                out.close(); //close stream for writing
                                System.out.println("line = " + line);
                                System.out.println("i = " + i);
                            } catch (javax.swing.text.BadLocationException ble) {
                                ble.getMessage();       
                                ble.printStackTrace();  
                            } catch (IOException ioe) {
                                ioe.getMessage();
                                ioe.printStackTrace();
                            }
    
    
                        }
                    } catch (IOException e) {
                        e.printStackTrace();   
                        e.getMessage();       
                    }
    
                    JOptionPane.showMessageDialog(this, "Save successful: " + fileName, "Saved .txt file", 2, null);
    
                } else {
                    System.out.println("Save command cancelled by user.");
                }
            }
        }                
    

    and here's my error:
    java.io.IOException: Stream closed
            at java.io.BufferedWriter.ensureOpen(BufferedWriter.java:98)
            at java.io.BufferedWriter.write(BufferedWriter.java:203)
            at java.io.Writer.write(Writer.java:140)
            at srcpkg.HTMLStripperMain.jButton4ActionPerformed(HTMLStripperMain.java:201)
            at srcpkg.HTMLStripperMain.access$300(HTMLStripperMain.java:40)
            at srcpkg.HTMLStripperMain$4.actionPerformed(HTMLStripperMain.java:94)
            at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1995)
            at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2318)
            at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:387)
            at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:242)
            at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:236)
            at java.awt.Component.processMouseEvent(Component.java:6041)
            at javax.swing.JComponent.processMouseEvent(JComponent.java:3265)
            at java.awt.Component.processEvent(Component.java:5806)
            at java.awt.Container.processEvent(Container.java:2058)
            at java.awt.Component.dispatchEventImpl(Component.java:4413)
            at java.awt.Container.dispatchEventImpl(Container.java:2116)
            at java.awt.Component.dispatchEvent(Component.java:4243)
            at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4322)
            at java.awt.LightweightDispatcher.processMouseEvent(Container.java:3986)
            at java.awt.LightweightDispatcher.dispatchEvent(Container.java:3916)
            at java.awt.Container.dispatchEventImpl(Container.java:2102)
            at java.awt.Window.dispatchEventImpl(Window.java:2440)
            at java.awt.Component.dispatchEvent(Component.java:4243)
            at java.awt.EventQueue.dispatchEvent(EventQueue.java:599)
            at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:273)
            at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:183)
            at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:173)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:168)
            at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:160)
            at java.awt.EventDispatchThread.run(EventDispatchThread.java:121)
    

    EDIT:
    Forgot to mention: If the JTextArea has the following:

    asdasdad
    cvbcvbcv
    adgdfgdf

    It will write the first line to the txt file and then throw the IOException error. i.e. it will write "asdasdad" to the text file and that's it.
    So close yet so far.. :)


Comments

  • Registered Users Posts: 4,188 ✭✭✭pH


    I haven't run it but you appear to be closing the output stream in your for loop - without running it I can't be sure but it seems to be your problem.

    out.close(); //close stream for writing


  • Registered Users Posts: 527 ✭✭✭Sean^DCT4


    pH wrote: »
    I haven't run it but you appear to be closing the output stream in your for loop - without running it I can't be sure but it seems to be your problem.

    out.close(); //close stream for writing

    thanks, I got it earlier on.
    I thought it was the out.newLine() or the "\n" I was trying.. I forgot I was still inside the for-loop.

    I think I need some time off :o

    thanks again


Advertisement