Requirement: Under a folder, search for all files of the type you want (e.g. I want all txt files in a folder).
ImportJava.io.File;/*** Get all files under the desired type of file *@authorXCX * @time June 24, 2017 PM 3:02:00*/ Public classGetFile { Public Static voidMain (string[] args) {String file= "D:\\java";//Name of the folderFile f =Newfile (file);//To create a file objectString extent = ". txt";//Want to end the. txt file hereGetFiles (f,extent); } Public Static voidGetFiles (File file,string extent) {//determine if the file is a directory if(File.isdirectory ()) {//file is a directoryfile[] files = file.listfiles ();//get all the files under the file//traverse these files to see if any files are being asked for(File f:files) {//traverse these files for recursionGetFiles (f, extent); } }Else{//file is not a directory//get the file path and decide if you want to end itString Path =File.getpath (); if(Path.endswith (extent)) {//is the required file, the output pathSystem.out.println (path); } } }}
This code can be used when searching for files.
Java Learning (1): Search for all files of the desired type under one folder