gpt4 book ai didi

java - 如何用Java制作文件过滤器?

转载 作者:IT老高 更新时间:2023-10-28 20:46:18 26 4
gpt4 key购买 nike

如何为 .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();
}

最佳答案

试试这样的...

String yourPath = "insert here your path..";
File directory = new File(yourPath);
String[] myFiles = directory.list(new FilenameFilter() {
public boolean accept(File directory, String fileName) {
return fileName.endsWith(".txt");
}
});

关于java - 如何用Java制作文件过滤器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5603966/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com