List All Text Files Present On Sd Card
I am using the following code to list down all the files present on the SD card. However I just want to list down all the text files can some body tell me how to add a file filter.
Solution 1:
Use .endsWith()
method from Java String Class to check File Extension from file path.
Method:
publicbooleanendsWith(String suffix)
Your Code something like,
private List<String> ReadSDCard()
{
//It have to be matched with the directory in SDCardFilef=newFile("your path");
File[] files=f.listFiles();
for(int i=0; i<files.length; i++)
{
Filefile= files[i];
/*It's assumed that all file in the path are in supported type*/StringfilePath= file.getPath();
if(filePath.endsWith(".txt")) // Condition to check .txt file extension
tFileList.add(filePath);
}
return tFileList;
}
Solution 2:
You can use FilenameFilter
Interface to Shortlist all TextFiles are any related files to an Array. You can check my answer here to shortlist only image files from SD card.
Solution 3:
import org.apache.commons.io.FilenameUtilsString extension=FilenameUtils.getExtension(filepath);
Post a Comment for "List All Text Files Present On Sd Card"