import java.awt.*;
import java.io.*;
import javax.swing.*;
//import javax.swing.event.*;
import java.awt.event.*;
import javax.swing.filechooser.FileFilter;
public class test extends JFrame
{
static int result;
static JFileChooser jf;
JButton jbt;
public test()
{
Container cp=getContentPane();
cp.setLayout(new FlowLayout(1));
jf=new JFileChooser();
jbt=new JButton("打开文件");
jf.setFileFilter(new FFT());
jbt.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
result=jf.showOpenDialog(null);
}
}
);
cp.add(jbt);
setSize(400,400);
setVisible(true);
}
public static void main(String[]args)throws IOException
{
File file=null;
String name;
test ap=new test();
ap.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
if(result == JFileChooser.APPROVE_OPTION)
{
file = jf.getSelectedFile();
FileInputStream f=new FileInputStream(file);
int n=f.available();
byte[] b=new byte[n];
while(f.read(b)==-1)
return;
String str=new String(b);
System.out.print(str);
f.close();
}
}
}
class FFT extends FileFilter
{
public boolean accept(File f)
{
if(f.getPath().endsWith(".txt")||f.isDirectory())
return true;
else
return false;
}
public String getDescription()
{
return "txt文件";
}
}