Java program to print all the file names starting with "Test-".
package fileio;
import java.io.File;
import java.io.FilenameFilter;
public class FileNames {
public static void main(String[] args) {
try {
final String filePrefix = "Test-";
String filesDir = System.getProperty("user.dir")+"\\src\\fileio";
File dir = new File(filesDir);
File[] foundFiles = dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith(filePrefix);
}
});
System.out.println("Number of files found : "+foundFiles.length);
System.out.println("");
for (File file : foundFiles) {
System.out.println(file.getName());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Executing the above Java Program:
Output:
package fileio;
import java.io.File;
import java.io.FilenameFilter;
public class FileNames {
public static void main(String[] args) {
try {
final String filePrefix = "Test-";
String filesDir = System.getProperty("user.dir")+"\\src\\fileio";
File dir = new File(filesDir);
File[] foundFiles = dir.listFiles(new FilenameFilter() {
public boolean accept(File dir, String name) {
return name.startsWith(filePrefix);
}
});
System.out.println("Number of files found : "+foundFiles.length);
System.out.println("");
for (File file : foundFiles) {
System.out.println(file.getName());
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Executing the above Java Program:
Output:
No comments:
Post a Comment