如何在Java中制作FileFilter?
2022-09-01 00:25:14
如何为.txt文件制作过滤器?
我写了这样的东西,但它有一个错误:
private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser chooser = new JFileChooser();
int retval = chooser.showOpenDialog(null);
String yourpath = "E:\\Programy Java\\Projekt_SR1\\src\\project_sr1";
File directory = new File(yourpath);
String[] myFiles;
FilenameFilter filter = new FilenameFilter() {
public boolean accept(File directory, String fileName) {
return fileName.endsWith(".txt");
}
};
myFiles = directory.list(filter);
if(retval == JFileChooser.APPROVE_OPTION)
{
File myFile = chooser.getSelectedFile();
}