[CODE]/*
* TestFile.java
*
* Created on 2007年8月10日, 下午10:21
*
* To change this template, choose Tools | Template Manager
* and open the template in the editor.
*/
package test1;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
/**
*
* @author hadeslee
*/
public class TestFile extends JFrame implements ActionListener{
private JTextArea jta;
private JButton jb,exit;
/** Creates a new instance of TestFile */
public TestFile() {
initWindow();
}
private void initWindow(){
jta=new JTextArea();
jta.setLineWrap(true);
jb=new JButton("打开文件");
exit=new JButton("退出");
exit.addActionListener(this);
jb.addActionListener(this);
this.add(new JScrollPane(jta),BorderLayout.CENTER);
JPanel jp=new JPanel();
jp.add(jb);
jp.add(exit);
this.add(jp,BorderLayout.SOUTH);
this.setSize(500,300);
this.setLocationRelativeTo(null);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
private File getSelectFile(Component com){
JFileChooser jfc=new JFileChooser();
int i=jfc.showOpenDialog(com);
if(i==JFileChooser.APPROVE_OPTION){
return jfc.getSelectedFile();
}else{
return null;
}
}
private String getContent(File file){
StringBuilder sb=new StringBuilder();
try{
BufferedReader br=new BufferedReader(new InputStreamReader(new FileInputStream(file)));
String temp=null;
while((temp=br.readLine())!=null){
sb.append(temp).append("\n");
}
}catch(Exception exe){
exe.printStackTrace();
}
return sb.toString();
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==jb){
File file=this.getSelectFile(this);
if(file!=null){
String s=this.getContent(file);
jta.setText(s);
}
}else if(e.getSource()==exit){
System.exit(0);
}
}
public static void main(String[] args) {
new TestFile();
}
}[/CODE]
自己看看吧,IO读取其实并不难