Finds all files under the directory and subdirectories under the given suffix name and prints the path to a file.

Source: Internet
Author: User
Tags parent directory
Package Filedemo;
Import Java.io.BufferedWriter;
Import Java.io.File;
Import Java.io.FileWriter;
Import Java.io.FilenameFilter;
Import java.io.IOException;

Import java.util.ArrayList;
	 public class Searchsuffixindir {/** * * Prints the file path of all the given suffix names under the given directory and subdirectories to a file. * * Depth traverse the parent directory to save the eligible file path to the collection.
	 (for filtering).
	 * * Then write the collection to the file.
		
		* */public static void main (string[] args) {file Dir = new File ("F:\\javatest");
		File File = new file (dir, "Suffix.txt"); 
				Internal class FilenameFilter Searchsuffix = new FilenameFilter () {@Override public boolean accept (File dir, String name) {			
			Return Name.endswith (". Java");
		
		}
		};
			
		arraylist<file> path = new arraylist<file> ();
		Writetoarr (Dir,searchsuffix,path);
	WriteToFile (Path,file);
		private static void WriteToFile (arraylist<file> al, file file) {BufferedWriter bw = null;
			
			try {bw = new BufferedWriter (new FileWriter (file)); for (File f:al) {new String ();//Bw.write (STring.valueof (F.length ()));
				Bw.write (F.getabsolutepath ());
				Bw.newline ();
			Bw.flush (); The catch (IOException e) {throw new RuntimeException ("Write failed.
		");
				}finally{if (BW!= null) try {bw.close (); The catch (IOException e) {throw new RuntimeException ("Shutdown failed.
				"); }} public static void Writetoarr (file dir, FilenameFilter Searchsuffix, arraylist<file> al) {file [
		]files = Dir.listfiles ();
				for (File f:files) {if (F.isdirectory ()) {//recursive.		
			Writetoarr (F, Searchsuffix, AL);
				}else{if (searchsuffix.accept (dir, F.getname ())) {Al.add (f);
 }
			}
		}
	}
	
	
	

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.