Tobias Moe Thorstensen
Tobias Moe Thorstensen

Reputation: 8981

FilenameFilter, searching for folders

I have this class that implements FilenameFilter

package Logic;

 import java.io.File;
 import java.io.FilenameFilter;

public class Filter implements FilenameFilter {

 String name; 

 public Filter(String name) {
    this.name = name; 

 }
 @Override
 public boolean accept(File dir, String filename) {
    return name.contains("bluetooth");
 }
}

I use this class in this method:

public String searchForBluetoothFolder() {

    String folderNameToSearchFor = "bluetooth";
    File root = sdCard; 
    FilenameFilter filter = new Filter(folderNameToSearchFor);  

    String[] bluetoothFolder = root.list(filter);

    for(int i = 0; i < bluetoothFolder.length; i++) {
        Log.i("Bluetooth: ", bluetoothFolder[i]); 
    }


    return "";
}

Inside the for-loop, the ouput is just all of the files in the root directory, not those who have bluetooth as name. What am I doing wrong here?

Upvotes: 4

Views: 3319

Answers (4)

Sujay
Sujay

Reputation: 6783

This is because you are checking whether name contains "bluetooth" and not whether the fileName contains the word "bluetooth"

return name.contains("bluetooth"); should be changed to return filename.contains("bluetooth");

However, by the way you're trying to implement, change it to return filename.contains(name); so that you do actually check whether your fileName contains the name that you've specified.

Also keep in mind that "bluetooth" might not be evaluated the same with contains() as "Bluetooth" or "blueTooth". If you want case-insensitive search, then I would suggest to standardize your name. Set name to lowercase and check using filename.toLowerCase().contains(name.toLowerCase()). Something like:

public class Filter implements FilenameFilter {
 String name; 

 public Filter(String name) {
    this.name = name; 

 }
 @Override
 public boolean accept(File dir, String filename) {
    //If you want to perform a case-insensitive search
    return filename.toLowerCase().contains(name.toLowerCase());
 }
}

Upvotes: 4

Lazy Ninja
Lazy Ninja

Reputation: 22527

package Logic;

 import java.io.File;
 import java.io.FilenameFilter;

public class Filter implements FilenameFilter {

 String name; 

 public Filter(String name) {
    this.name = name; 

 }
 @Override
 public boolean accept(File dir, String filename) {
    return filename.contains("bluetooth");
 }
}

Upvotes: 1

Georgy Gobozov
Georgy Gobozov

Reputation: 13721

FileFilter folderFilter = new FileFilter() {
    @Override
    public boolean accept(File file) {
        return file.isDirectory() && file.getName().contains("bluetooth");
    }
};

File[] files = f.listFiles(folderFilter);

Upvotes: 1

Daniel Pereira
Daniel Pereira

Reputation: 2785

You are calling the method contains in the attribute name, call it in the parameter filename instead. You also should remove the literal string from the method, doing like this:

filename.contains(name)

This way you are using the attribute that you created for it, and can reuse the class in another cases. The same applies to the searchForBluetoothFolder method.

Upvotes: 1

Related Questions