Issue using IOFileFilter - java

i have used Apache FileUtils and IOFileFilter to list all files under a folder recursively excluding .svn folders. Here is the code i tried
File selectedFolder = new File(path);\\path to folder to list
final IOFileFilter dirs = new IOFileFilter() {
#Override
public boolean accept(File file, String s) {
return file.isDirectory();
}
#Override
public boolean accept(File file) {
// TODO Auto-generated method stub
if(file.getName().toLowerCase().equalsIgnoreCase(".svn")||file.getName().toLowerCase().contains(".svn"))
return false;
else return true;
}
};
filesList.addAll(FileUtils.listFiles(selectedFolder,dirs, TrueFileFilter.INSTANCE));
I am getting the error
java.lang.IllegalArgumentException: Parameter 'directory' is not a directory
at org.apache.commons.io.FileUtils.validateListFilesParameters(FileUtils.java:545)
at org.apache.commons.io.FileUtils.listFiles(FileUtils.java:521)
Can anyone tell me where am going wrong. I feel there is something wrong with the filter used. I could not figure it out

Actually, FileFilterUtils contains a method called makeSVNAware that you could use. It returns a filter that ignores SVN directories. For example:
filesList.addAll(
FileUtils.listFiles(selectedFolder, TrueFileFilter.TRUE,
FileFilterUtils.makeSVNAware(null)));
Note that listFiles expects a file filter as its 2nd argument, and a dir filter as its 3rd. In your code they're the other way round. So if you wouldn't want to use makeSVNAware, your code would look something like this:
File selectedFolder = new File(path); // path to folder to list
final IOFileFilter dirs = new IOFileFilter() {
#Override
public boolean accept(File file, String s) {
return file.isDirectory();
}
#Override
public boolean accept(File file) {
return (!file.getName().toLowerCase().equalsIgnoreCase(".svn"));
}
};
// 2nd argument: TRUE filter, returning all files
// 3rd argument: dirs filter, returning all directories except those named .svn
filesList.addAll(FileUtils.listFiles(selectedFolder, TrueFileFilter.TRUE, dirs));

It looks like you have split the functionality into two functions.
The second one should also check for isDirectory() and the first one should also check the name.

Related

How to select all files in a FileDialog?

I want to select all files to exclude them from being displayed in my FileDialog.
FileDialog fileDialog = new FileDialog(this, "Some Title", FileDialog.LOAD);
fileDialog.setFilenameFilter(new FilenameFilter() {
#Override
public boolean accept(File dir, String name) {
if(name.endsWith(".*")) {
return false;
}else {
return true;
}
}
});
fileDialog.setVisible(true);
In my code you can see, that I am trying to to that with the String ".*", to select all files. This doesn't work however and I don't know why.
I only want to show directories.
Thanks for your help!
You can use a JFileChooser, using a FileFilter to check the File object to see if it is a directory
#Override
public boolean accept( File file ) {
return file.isDirectory();
}
A FileDialog's FileFilter should work similarly. Also note the API for the FileDialog's setFileFilter method:
"Filename filters do not function in Sun's reference implementation for Microsoft Windows."
As previously stated, I found the answer to this question with the help of #JigarJoshi.
This is the working code to show ONLY Directories on a AWT FileDialog:
fileDialog.setFilenameFilter(new FilenameFilter() {
#Override
public boolean accept(File dir, String name) {
return dir.isFile();
}
});
Please note that using FileDialog over JFileChooser is only recommendet, if you are on a non-Windows system. However on Mac and Linux, you should prefer to use FileDialog since it looks more native.
Thank you very much for your input!

Why is my Iterator empty?

I have an Iterator that should be being populated by the FileUtils.iterateFiles() function using an instance of IOFileFilter to sort through and return an iterator of files matching the title "sound".
The problem is, even though i can see the accept(File file) override in the IOFileFilter returning true (via breakpoint), when i query iterator.hasNext(), I get false, and debug shows that the iterator is empty. Can anybody spot any possible causes? Or do Iterators and IOFileFiltters not behave in the way in which i think they do?
Code below.
private IOFileFilter findSoundsDir = new IOFileFilter()
{
#Override
public boolean accept(File file) {
return file.getName().equals("sounds"); //Breakpoint here gets hit
}
#Override
public boolean accept(File file, String s) {
return s.equals("sounds");
}
};
public void generateSoundEmbeds(String baseDir)
{
File searchingDir = new File(baseDir).getParentFile().getParentFile();
System.out.println("searchingDir: " + searchingDir.getAbsolutePath());
Iterator<File> soundsIterator = (Iterator<File>)FileUtils.iterateFiles(searchingDir, findSoundsDir ,TrueFileFilter.INSTANCE);
if(soundsIterator.hasNext()) //however this check returns false, despite IOFileFilter returning true at least once previously
{
//we never get here
}else{
System.out.println("found no file");
}
}

List all files of one type in android

I need to retrieve a list of all files of a certain type on my internal and external storage. I found this (-> List all of one file type on Android device?) example, but it's very mp3 specific.
My app creates a .zip file which is renamed to the extension *.message
Another activity should display a list of all available .message files, from which the user can choose one to open it.
Does anyone has an idea how to begin?
Thanks!
To obtain a list of files with a specific extension you can use File.list() with a FilenameFilter. For example:
File dir = new File(".");
String[] names = dir.list(
new FilenameFilter()
{
public boolean accept(File dir, String name)
{
return name.endsWith(".message");
}
});
Note that the returned strings are file names only, they do not contain the full path.
Here it is
File root = new File(globalVar.recordDir);
{
try {
if (root.listFiles().length > 0) {
for (File file : root.listFiles()) {
if (file.getName().endsWith(".mp3")) {
//DO somthings with it
}
}
}
} else {
}
} catch (Exception e) {
}
}

JFileChooser Help

I am trying to set the file filter for my JFileChooser. This is my code:
JFileChooser picker= new JFileChooser();
picker.setFileFilter(new FileNameExtensionFilter("txt"));
int pickerResult = picker.showOpenDialog(getParent());
if (pickerResult == JFileChooser.APPROVE_OPTION){
System.out.println("This works!");
}
if (pickerResult == JFileChooser.CANCEL_OPTION){
System.exit(1);
}
When I run my program, the file chooser comes up, but it won't let me pick any .txt files. Instead, it says this in the console:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Extensions must be non-null and not empty
How do i fix this?
You need to add at least one extension as a second paramter. From the API:
FileNameExtensionFilter(String description, String... extensions)
Parameters:
description - textual description for the filter, may be null
extensions - the accepted file name extensions
Also if you want an specific files extensions and navigate thru folders you can try this:
JFileChooser fc = new JFileChooser(path);
fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
fc.addChoosableFileFilter(new FileFilter () {
#Override
public String getDescription() {
return "DAT Files";
}
#Override
public boolean accept(File f) {
if (f.isDirectory())
return true;
return f.getName().endsWith(".dat");
}
});
fc.setAcceptAllFileFilterUsed(false);

remove allfile option in jFilechosser java

i'm using swings jfilechooser in program,i want it to filter files with .txt extension,and it is showing allfiles option also in window,so i want to remove allfiles option,how can i do it plz help me
this is my code:
fc1 = new JFileChooser();
fc1.setMultiSelectionEnabled(true); // Allow for multiple selections
fc1.setCurrentDirectory(new File("C:\\"));
fc1.setFileFilter(new FileFilter() {
public boolean accept(File f) {
return f.getName().toLowerCase().endsWith(".fls")
|| f.isDirectory();
}
public String getDescription() {
// TODO Auto-generated method stub
return "*.fls";
}
});
Thanks in adavance
mukta
Well, just by reading the javadoc, I can see that there's a method called setAcceptAllFileFilterUsed(boolean). Did you try that? It sounds like it's doing what you want.

Categories

Resources