like in title how to make filter to .txt files?
i wrote something like this but it has error :(
private void jMenuItem1ActionPerformed(java.awt.even
You are going wrong here:
int retval = chooser.showOpenDialog(null);
public boolean accept(File directory, String fileName) {`
return fileName.endsWith(".txt");`
}
You first show the file chooser dialog and then apply the filter! This wont work. First apply the filter and then show the dialog:
public boolean accept(File directory, String fileName) {
return fileName.endsWith(".txt");
}
int retval = chooser.showOpenDialog(null);
... What about using Apache's FileFilters from:
org.apache.commons.io
?
just like:
import org.apache.commons.io.filefilter.FileFileFilter;
import org.apache.commons.io.filefilter.WildcardFileFilter;
...
File dir = new File(".");
String[] filters = { "*.txt"};
IOFileFilter wildCardFilter = new WildcardFileFilter(filters, IOCase.INSENSITIVE);
String[] files = dir.list( wildCardFilter );
for ( int i = 0; i < files.length; i++ ) {
System.out.println(files[i]);
}
Here is a little utility class that I created:
import java.io.File;
import java.io.FilenameFilter;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
/**
* Convenience utility to create a FilenameFilter, based on a list of extensions
*/
public class FileExtensionFilter implements FilenameFilter {
private Set<String> exts = new HashSet<String>();
/**
* @param extensions
* a list of allowed extensions, without the dot, e.g.
* <code>"xml","html","rss"</code>
*/
public FileExtensionFilter(String... extensions) {
for (String ext : extensions) {
exts.add("." + ext.toLowerCase().trim());
}
}
public boolean accept(File dir, String name) {
final Iterator<String> extList = exts.iterator();
while (extList.hasNext()) {
if (name.toLowerCase().endsWith(extList.next())) {
return true;
}
}
return false;
}
}
Usage:
String[] files = new File("myfile").list(new FileExtensionFilter("pdf", "zip"));
From JDK8 on words it is as simple as
final String extension = ".java";
final File currentDir = new File(YOUR_DIRECTORY_PATH);
File[] files = currentDir.listFiles((File pathname) -> pathname.getName().endsWith(extension));
Here you will find some working examples. This is also a good example of FileFilter used in JFileChooser.
The basics are, you need to override FileFilter class and write your custom code in its accpet method. The accept method in above example is doing filtration based on file types:
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
} else {
String path = file.getAbsolutePath().toLowerCase();
for (int i = 0, n = extensions.length; i < n; i++) {
String extension = extensions[i];
if ((path.endsWith(extension) && (path.charAt(path.length()
- extension.length() - 1)) == '.')) {
return true;
}
}
}
return false;
}
Or more simpler to use is FileNameFilter which has accept method with filename as argument, so you don't need to get it manually.
Since Java7 you can simply use FileNameExtensionFilter(String description, String... extensions)
A simple JFileChooser analog to the example would be:
JFileChooser fileChooser = new JFileChooser();
fileChooser.setFileFilter(new FileNameExtensionFilter("Text files", "txt"));
I know the question was answered long ago, but this is actually the simplest solution.