节选一:
package com.test.filetest;
import java.io.File;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileFilter;
public class FileTest {
public static void main(String[] args) {
// 创建文件选择器
JFileChooser fileChooser = new JFileChooser();
// 设置当前目录
fileChooser.setCurrentDirectory(new File("."));
fileChooser.setAcceptAllFileFilterUsed(false);
final String[][] fileENames = { { ".java", "JAVA源程序 文件(*.java)" },
{ ".doc", "MS-Word 2003 文件(*.doc)" },
{ ".xls", "MS-Excel 2003 文件(*.xls)" }
};
// 显示所有文件
fileChooser.addChoosableFileFilter(new FileFilter() {
public boolean accept(File file) {
return true;
}
public String getDescription() {
return "所有文件(*.*)";
}
});
// 循环添加需要显示的文件
for (final String[] fileEName : fileENames) {
fileChooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
public boolean accept(File file) {
if (file.getName().endsWith(fileEName[0]) || file.isDirectory()) {
return true;
}
return false;
}
public String getDescription() {
return fileEName[1];
}
});
}
fileChooser.showDialog(null, null);
}
}
节选二:
button.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
//产生一个文件选择器
jChooser = new JFileChooser();
//设置默认的打开目录,如果不设的话按照window的默认目录(我的文档)
jChooser.setCurrentDirectory(new File("e:/"));
//设置打开文件类型,此处设置成只能选择文件夹,不能选择文件
jChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);//只能打开文件夹
//打开一个对话框
int index = jChooser.showDialog(null, "打开文件");
if (index == JFileChooser.APPROVE_OPTION) {
//把获取到的文件的绝对路径显示在文本编辑框中
jt.setText(jChooser.getSelectedFile().getAbsolutePath());
readPath = jt.getText() + "\\";
}
}
});
2.点击保存按钮后,打开一个保存对话框
button2.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent arg0) {
// TODO Auto-generated method stub
jChooser2 = new JFileChooser();
jChooser2.setCurrentDirectory(new File("e:/"));//设置默认打开路径
jChooser2.setDialogType(JFileChooser.SAVE_DIALOG);//设置保存对话框
//将设置好了的两种文件过滤器添加到文件选择器中来
TxtFileFilter txtFileFilter = new TxtFileFilter();
XlsFileFilter xlsFileFilter = new XlsFileFilter();
jChooser2.addChoosableFileFilter(txtFileFilter);
jChooser2.addChoosableFileFilter(xlsFileFilter);
int index = jChooser2.showDialog(null, "保存文件");
if (index == JFileChooser.APPROVE_OPTION) {
File f = jChooser2.getSelectedFile();
String fileName = jChooser2.getName(f) + ".xls";
writePath = jChooser2.getCurrentDirectory().getAbsolutePath() + fileName;
try {
writeFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//程序执行完毕后,出现一个对话框来提示
int option = JOptionPane.showConfirmDialog(null, "贱人:结果已产生", "结果",
JOptionPane.YES_NO_OPTION);
System.exit(0);
}
}
});
//重写文件过滤器,设置打开类型中几种可选的文件类型,这里设了两种,一种txt,一种xls
class TxtFileFilter extends FileFilter {
@Override
public boolean accept(File f) {
// TODO Auto-generated method stub
String nameString = f.getName();
return nameString.toLowerCase().endsWith(".txt");
}
@Override
public String getDescription() {
// TODO Auto-generated method stub
return "*.txt(文本文件)";
}
}
class XlsFileFilter extends FileFilter {
@Override
public boolean accept(File f) {
// TODO Auto-generated method stub
String nameString = f.getName();
return nameString.toLowerCase().endsWith(".xls");
}
@Override
public String getDescription() {
// TODO Auto-generated method stub
return "*.xls(表格文件)";
}
}