当前位置: 技术问答>java相关
有谁可以提供一个简单的class给我.要求是打开一个指定路径文本文件.把它的内容读出来.用System.out.println显示
来源: 互联网 发布时间:2017-04-06
本文导语: 要求1.文件的路径自己定义 要求2.要显示文件的大小 要求3.把文件的内容读出来 | //try this code below //this code using JTextArea to show your .txt file import java.io.*; import java.awt.*; import java.awt.event.*; import jav...
要求1.文件的路径自己定义
要求2.要显示文件的大小
要求3.把文件的内容读出来
要求2.要显示文件的大小
要求3.把文件的内容读出来
|
//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);
}
}
}