import java.io.*; import java.util.*; public class getFiles implements FilenameFilter { String str = ""; public getFiles(String s) { str = s; } // Override default accept method, which checks if a file should be included in the list public boolean accept(File f, String s) { return s.toLowerCase().endsWith(str.toLowerCase()); } public static void main(String[] args) throws IOException { Scanner stdin = new Scanner(System.in); File f = new File("."); String ext = ""; // Ask user for the file extension and set it into a String variable System.out.println("Enter the file extension: "); ext = stdin.next(); // Create a string array, filled with a file listing of the current directory getFiles gf = new getFiles(ext); String strarr[] = f.list(gf); // Files were found if (strarr.length > 1) { // Shuffle the array into a random order Collections.shuffle(Arrays.asList(strarr)); try { // File to write to PrintWriter pw = new PrintWriter(new FileWriter("list.txt")); // Loop through the array and write each entry to the file for (int i = 1; i <= strarr.length-1; i++) pw.println(strarr[i].substring(0,strarr[i].length()-(ext.length()+1))); // Close the file pw.close(); } catch (IOException i) { i.printStackTrace(); } } // No files were found else { System.out.println("No files found with extension " + ext); return; } } }