String folderpath = "G:\\AE_IntegrationComp";
//Above is Folder where is Different files are present
List <String>filet = new ArrayList<String>();
filet.add(".txt");
filet.add(".doc");
//extension which I added
for(String str : filet)
{
}
File directory = new File(folderpath);
for(File list : directory.listFiles())
{
if(list.getName().contains(""))
{
System.out.println(list.getName());
}
}
I have to check if Directory is empty or not if not, file extension in Arraylist should matched with extensions Are available in Directory and print files that matched
CodePudding user response:
What you wanna do is:
- Iterate through all files into a directory (which you have done)
- Check if it has a certain extension (it ends with a certain string)
- Print the file name if that matches.
public class Main {
// Here we have a constant containing the interesting file extensions
public static final String[] extensions = new[] { ".txt", ".doc" };
// This helper function will tell us whether
// a file has one of the interesting file extensions
public static boolean matchesExtension(File file) {
for(String ext : extensions) {
if(file.getName().endsWith(ext)) {
return true;
}
}
return false;
}
public static void main(String[] args) {
String folderpath = "G:\\AE_IntegrationComp";
File directory = new File(folderpath);
// Iterate through all files in the directory
for(File file : directory.listFiles()){
if(matchesExtension(file)){
System.out.println(file.getName());
}
}
}
}