package filehandling;
import java.awt.event.ActionEvent;
import java.io.*;
import java.awt.*;
import java.awt.event.ActionListener;
import javax.swing.*;


public class Filehandling extends JFrame{
 JTextArea ja;
 JButton jb;
    Filehandling()
 {
 setBounds(400,400,400,400);
 setTitle("azeem");
 setVisible(true);
 setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  Container cont= getContentPane();
  ja=new JTextArea ();
  cont.add(ja,BorderLayout.CENTER);
  JPanel jp= new JPanel();
  jb=new JButton ("Save");
  jp.add(jb);
  cont.add(jp,BorderLayout.SOUTH);
  setVisible(true);

 }





    public static void main(String[] args) {
        Filehandling fh= new Filehandling();
    }
    class theHandler implements ActionListener 

    {

        @Override
        public void actionPerformed(ActionEvent e) {
           if (e.getSource()==jb)
           {
               //Error is here
           function12(ja.getText());


           }
        }
        public String function12(String man) throws Exception
    {
    File f=new File("File.txt");
       FileOutputStream ob=new FileOutputStream(f.getName());
       byte [] bob= man.getBytes();
       ob.write(bob);
       ob.close();
       return man;
    }



    }



}

Dani AI

Generated

A few targeted fixes and a safer pattern for saving a JTextArea to disk.

correctly flagged the checked-exception/handler issue and pointed to the component API. A compact, robust approach: attach an ActionListener to the Save button, grab the JTextArea text on the EDT, then perform the file I/O off the EDT (SwingWorker) while using try-with-resources and an explicit charset so the stream is closed and the encoding is defined.

jb.addActionListener(new ActionListener() {
    @Override
    public void actionPerformed(ActionEvent e) {
        final String text = ja.getText();
        new SwingWorker<Void,Void>() {
            @Override
            protected Void doInBackground() throws Exception {
                Path out = Paths.get(System.getProperty("user.home"), "File.txt");
                try (BufferedWriter w = Files.newBufferedWriter(out, StandardCharsets.UTF_8)) {
                    w.write(text);
                }
                return null;
            }
            @Override
            protected void done() {
                try { get(); } catch (Exception ex) { ex.printStackTrace(); }
            }
        }.execute();
    }
});

Why this helps: try-with-resources guarantees close/flush; specifying UTF-8 avoids platform surprises; using Paths/Files avoids manual byte-array handling; SwingWorker prevents UI freezes on large text; actionPerformed cannot declare checked exceptions, so either catch them there or handle them in done(). Avoid calling setVisible(true) before building the UI; create the GUI on the EDT with SwingUtilities.invokeLater. For user-selected locations, use JFileChooser instead of hard-coding a path. If preserving the JTextArea's exact formatting/metadata is required, consider the JTextComponent write(Writer) helper mentioned by , but still wrap the Writer with an explicit charset and resource management.

Quick checklist: add the listener (as above), use try-with-resources, pick an encoding, run heavy I/O off the EDT, and let the user choose the file location.

Recommended Answers

All 7 Replies

function12 throws an Exception, but you never catch or throw it upwards when you call that method

I think that you need to use an loop to write the bytes from "man" into the output stream. Also try to flush the outputstream before you close it.

No need for a loop - the write method has a variant that takes an array of bytes (see the API doc).
flush() is more interesting. You would expect that FileOutputStream would flush as part of its close() method, but the API doc does not say anything on the subject. Although we all know that, in practice, close() is enough for a FileOutputStream, a properly paranoid programmer shoud call flush() just to be sure ;)

haider885: Looks like your function is not being called at all. The code doesnt reach there. Flow ends with creating the text area. Please check

haider885: When I said function, I meant the method function12.

It is called from the event handler, but I can't see the necessary call to addActionListener to make that work!

I want to copy TextArea text to a File... == for me (including properties from NativeOS, newline, tab, .....) is JTextArea.write

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.