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;
    }



    }



}

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.