mrN
mrN

Reputation: 3774

Loading a text file into a textarea

First of all, I am very basic at java. I am trying to browse a .txt file and load the contents of it, into the text area. I am completed the part, till which I receive the file from the JFileChooser, now I dont know how to do the remaining.

.
.
.

File selFile = new File(fileChooser.getSelectedfile());
/// From here I want to load its content to a textarea "txtArea"

Upvotes: 4

Views: 55546

Answers (5)

Maxim
Maxim

Reputation: 1

try {
    BufferedReader reader = new BufferedReader(new FileReader("notes.nwp"));
    data = reader.read();
    while (data != -1)
    {
        // print the character to the screen
        k = new JTextArea((char)data);
        data = reader.read();
    }
    reader.close();
}
catch(IOException e1) {

}

Upvotes: 0

camickr
camickr

Reputation: 324108

Use the read(...) and write(...) methods that are suppoorted by all Swing text components. Simple example:

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.text.*;

class TextAreaLoad
{
    public static void main(String a[])
    {
        final JTextArea edit = new JTextArea(10, 60);
        edit.setText("one\ntwo\nthree");
        edit.append("\nfour\nfive");

        JButton read = new JButton("Read TextAreaLoad.txt");
        read.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                try
                {
                    FileReader reader = new FileReader( "TextAreaLoad.txt" );
                    BufferedReader br = new BufferedReader(reader);
                    edit.read( br, null );
                    br.close();
                    edit.requestFocus();
                }
                catch(Exception e2) { System.out.println(e2); }
            }
        });

        JButton write = new JButton("Write TextAreaLoad.txt");
        write.addActionListener( new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                try
                {
                    FileWriter writer = new FileWriter( "TextAreaLoad.txt" );
                    BufferedWriter bw = new BufferedWriter( writer );
                    edit.write( bw );
                    bw.close();
                    edit.setText("");
                    edit.requestFocus();
                }
                catch(Exception e2) {}
            }
        });

        JFrame frame = new JFrame("TextArea Load");
        frame.getContentPane().add( new JScrollPane(edit), BorderLayout.NORTH );
        frame.getContentPane().add(read, BorderLayout.WEST);
        frame.getContentPane().add(write, BorderLayout.EAST);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo( null );
        frame.setVisible(true);
    }
}

Upvotes: 13

Obuli Sundar
Obuli Sundar

Reputation: 189

For indentation and line break you have to use "\n" before appending to to the text area..

  BufferedReader buff = null;
  try {
       buff = new BufferedReader(new FileReader(selFile));
       String str;
       while ((str = buff.readLine()) != null) {
       jtextArea.append("\n"+str);
   }
 } catch (IOException e) {
  } finally {
    try { in.close(); } catch (Exception ex) { }
    }

Upvotes: 0

Vijay Mathew
Vijay Mathew

Reputation: 27174

BufferedReader in = null;
try {
    in = new BufferedReader(new FileReader(selFile));
    String str;
    while ((str = in.readLine()) != null) {
        jtextArea.append(str);
    }
} catch (IOException e) {
} finally {
    try { in.close(); } catch (Exception ex) { }
}

Upvotes: 2

Qwerky
Qwerky

Reputation: 18435

Use a BufferedReader to read the .txt file line by line. You can then append each line to your text area.

Upvotes: -1

Related Questions