import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
public class JFileChooserTest {
public static void main(String[] args) {
FrameFileDialog f = new FrameFileDialog();
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
class FrameFileDialog extends JFrame implements ActionListener {
private JFileChooser fileDialog = null;
private Label label = new Label("INFO:", Label.CENTER);
private JButton b1, b2;
private JTextArea textArea = null;
public FrameFileDialog() {
super("带文件对话框的窗口");
Container con = this.getContentPane();
con.setLayout(new BorderLayout());
con.setSize(40, 50);
b1 = new JButton("打开文件");
b2 = new JButton("保存文件");
b1.addActionListener(this);
b2.addActionListener(this);
JPanel p = new JPanel();
p.add(b1);
p.add(b2);
textArea = new JTextArea(20, 30);
JScrollPane jsp = new JScrollPane(textArea);
fileDialog = new JFileChooser("C:\\");
fileDialog.setControlButtonsAreShown(true);
// 设置过滤条件
fileDialog.setFileFilter(new MyFileFilter("java"));
fileDialog.setFileFilter(new MyFileFilter("txt"));
con.add(jsp, BorderLayout.CENTER);
con.add(label, BorderLayout.NORTH);
con.add(p, BorderLayout.SOUTH);
this.setSize(380,280);
this.setVisible(true);
//this.pack();
}
@Override
// 监听器
public void actionPerformed(ActionEvent e) {
File file = null;
int result;
if (e.getSource() == b1) {// OPEN
fileDialog.setDialogTitle("打开文件");
result = fileDialog.showOpenDialog(this);
textArea.setText("");
if (result == JFileChooser.APPROVE_OPTION) {
file = fileDialog.getSelectedFile();
label.setText("选择的文件是:" + file.getName());
} else if (result == JFileChooser.CANCEL_OPTION) {
//JOptionPane.showMessageDialog(this, "你还未选择文件");
label.setText("未选中文件");
return;
}
//选择了文件
FileInputStream fileStream = null;
try {
fileStream = new FileInputStream(file);
} catch (FileNotFoundException e1) {
label.setText("文件没找到");
e1.printStackTrace();
return;
}
int readByte;
byte buffer[]=new byte[1024];
try {
while ((readByte = fileStream.read(buffer)) !=-1) {
textArea.append(new String(buffer,0,readByte));
}
//流使用完后关闭
fileStream.close();
} catch (IOException e1) {
label.setText("读取文件出错");
e1.printStackTrace();
}
} else if (e.getSource() == b2) {// 保存文件
fileDialog.setDialogTitle("保存文件");
result = fileDialog.showSaveDialog(this);
file = null;
if (result == JFileChooser.APPROVE_OPTION) {
file = fileDialog.getSelectedFile();
label.setText("保存的文件是:" + file.getName());
} else if (result == JFileChooser.CANCEL_OPTION) {
label.setText("没有选择任何文件");
}
FileOutputStream fileStream1 = null;
PrintWriter pw=null;
if (file != null) {
try {
fileStream1 = new FileOutputStream(file);
} catch (FileNotFoundException e1) {
label.setText("文件没有发现");
e1.printStackTrace();
return;
}
String content = textArea.getText();
try {
pw=new PrintWriter(fileStream1);
pw.print(content);
pw.close();
// fileStream1.write(content.getBytes());
fileStream1.close();
} catch (IOException e1) {
label.setText("写文件出错");
e1.printStackTrace();
}
}
}
}
}
/**
* 文件过滤器,让文件打开对话框只显示特定格式的文件
* @author Administrator
*
*/
class MyFileFilter extends FileFilter {
private String ext;
MyFileFilter(String ext) {
this.ext = ext;
}
@Override
public boolean accept(File file) {
if (file.isDirectory())
return true;
String fileName = file.getName();
int index = fileName.lastIndexOf(".");
if (index > 0 && index < fileName.length() - 1) {
String extension = fileName.substring(index + 1);
if (extension.toLowerCase().equals(ext))
return true;
}
return false;
}
@Override
public String getDescription() {
if (ext.equals("java")) {
return "Java source file(*.java)";
}
if (ext.equals("txt")) {
return "TXT file(*.txt)";
}
return "";
}
}