//try this code below
//this code using JTextArea to show your .txt file
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Test4 extends JFrame
implements ActionListener{ JLabel label1;
JButton enterButton;
JTextField enterField;
JTextArea outputArea; public Test4()
{
super ("Inner class Demonstration"); Container container = getContentPane();
container.setLayout(new FlowLayout()); label1 = new JLabel ("Enter file name" );
container.add(label1); enterField = new JTextField (10);
container.add(enterField);
enterField.addActionListener(this) ; enterButton = new JButton("Read");
container.add(enterButton);
enterButton.addActionListener(this) ; outputArea = new JTextArea (8,20);
container.add(outputArea);
} public static void main(String args[])
{
Test4 window = new Test4();
window.setSize(300,250);
window.setVisible(true);
} public void actionPerformed (ActionEvent actionEvent)
{
String fileName = enterField.getText();
String output = "";
StringBuffer buffer = new StringBuffer();
try
{
// initialise the file to be read.
FileInputStream in = new FileInputStream(fileName);
// now create the object of Buffered Reader.
InputStreamReader stream = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(stream); while ((output = reader.readLine())!= null)
buffer.append(output + "\n"); outputArea.setText(buffer.toString()); in.close();
} catch (IOException ioException)
{
JOptionPane.showMessageDialog(null,"File error","File error",JOptionPane.ERROR_MESSAGE ) ;
} catch (Exception exception)
{
JOptionPane.showMessageDialog(null,"error","error",JOptionPane.ERROR_MESSAGE);
}
}
}
//this code using JTextArea to show your .txt file
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Test4 extends JFrame
implements ActionListener{ JLabel label1;
JButton enterButton;
JTextField enterField;
JTextArea outputArea; public Test4()
{
super ("Inner class Demonstration"); Container container = getContentPane();
container.setLayout(new FlowLayout()); label1 = new JLabel ("Enter file name" );
container.add(label1); enterField = new JTextField (10);
container.add(enterField);
enterField.addActionListener(this) ; enterButton = new JButton("Read");
container.add(enterButton);
enterButton.addActionListener(this) ; outputArea = new JTextArea (8,20);
container.add(outputArea);
} public static void main(String args[])
{
Test4 window = new Test4();
window.setSize(300,250);
window.setVisible(true);
} public void actionPerformed (ActionEvent actionEvent)
{
String fileName = enterField.getText();
String output = "";
StringBuffer buffer = new StringBuffer();
try
{
// initialise the file to be read.
FileInputStream in = new FileInputStream(fileName);
// now create the object of Buffered Reader.
InputStreamReader stream = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(stream); while ((output = reader.readLine())!= null)
buffer.append(output + "\n"); outputArea.setText(buffer.toString()); in.close();
} catch (IOException ioException)
{
JOptionPane.showMessageDialog(null,"File error","File error",JOptionPane.ERROR_MESSAGE ) ;
} catch (Exception exception)
{
JOptionPane.showMessageDialog(null,"error","error",JOptionPane.ERROR_MESSAGE);
}
}
}
//this code using System.out to show your .txt file in your cosole
//windowimport java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;public class Test5 extends JFrame
implements ActionListener{ JLabel label1;
JTextField enterField; public Test5()
{
super ("Inner class Demonstration"); Container container = getContentPane();
container.setLayout(new FlowLayout()); label1 = new JLabel ("Enter file name" );
container.add(label1);
enterField = new JTextField (10);
container.add(enterField);
enterField.addActionListener(this) ;
} public static void main(String args[])
{
Test5 window = new Test5();
window.setSize(300,70);
window.setVisible(true);
} public void actionPerformed (ActionEvent actionEvent)
{
String fileName = enterField.getText();
String output = "";
StringBuffer buffer = new StringBuffer();
try
{
// initialise the file to be read.
FileInputStream in = new FileInputStream(fileName);
// now create the object of Buffered Reader.
InputStreamReader stream = new InputStreamReader(in);
BufferedReader reader = new BufferedReader(stream); while ((output = reader.readLine())!= null)
buffer.append(output + "\n"); System.out.println(buffer.toString()); in.close();
} catch (IOException ioException)
{
JOptionPane.showMessageDialog(null,"File error","File error",JOptionPane.ERROR_MESSAGE ) ;
} catch (Exception exception)
{
JOptionPane.showMessageDialog(null,"error","error",JOptionPane.ERROR_MESSAGE);
}
}
}