Reading and Writing a File in JTextArea

Image of writing
Well in this post i will introduce two methods that will read and write the JTextArea document to a file.

Reading a file is as done in the drag and drop in JTextArea post. Writing is what you might see new here in this post.

read(Reader r,Object desc): The first parameter is a java.io.Reader object and the next one is of type Object which is the description of the stream. You can even write null here.
write(Writer out): This method takes the java.io.Writer object which points out to a file where the content in the JTextArea has to be written.


import java.io.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class ReadWriteJTextArea extends JFrame
{
private JTextArea jt;
private JScrollPane js;
private JButton read,write;

    public ReadWriteJTextArea()
    {
        createAndShowGUI();
    }
   
    private void createAndShowGUI()
    {
        setTitle("Read and Write in JTextArea");
        setSize(400,400);
        setLayout(new FlowLayout());
       
        setVisible(true);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
       
       
       
        jt=new JTextArea(25,50);
        js=new JScrollPane(jt);
        add(js);
       
        read=new JButton("Read");
        add(read);
        read.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae)
            {
                readActionPerformed();
            }
        });
       
        write=new JButton("Write");
        add(write);
        write.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent ae)
            {
                writeActionPerformed();
            }
        });
       
       
        setLocationRelativeTo(null);
    }
   
    private File createJFileChooser(boolean open)
    {
        int option;
        JFileChooser jf=new JFileChooser();
       
        if(open)
        option=jf.showOpenDialog(this);
        else
        option=jf.showSaveDialog(this);
       
            if(option==JFileChooser.APPROVE_OPTION) return jf.getSelectedFile();
           
    return null;
    }
   
    private void readActionPerformed()
    {
        try
        {
        jt.read(new FileReader(createJFileChooser(true)),null);
        }catch(Exception e){}
    }
   
    private void writeActionPerformed()
    {
        try
        {
        jt.write(new FileWriter(createJFileChooser(false)));
        }catch(Exception e){}
    }
   
    public static void main(String args[])
    {
        new ReadWriteJTextArea();
    }
}

Output of ReadWriteJTextArea

Screenshot of the ReadAndWriteJTextArea.java

Explaining Reading and Writing

There is in fact nothing to explain as said above. I only would like to point out that instead of writing our own code which is superfluous, it is better to make use of the methods provided by the Java developers. Share the post and be a part of the post's viral success!

Image Credit on Flickr: jjpacres

No comments: