Java I/o---Get the file directory and write to text

Source: Internet
Author: User

First gets all the file directories under the specified directory, saves them in the list collection, and then creates a text file that will be saved in the list traversal write text.

1. Main Program Class
1  Public classTest {2 3 /** 4 * @param args 5 * *6  Public Static voidMain (string[] args) {7 //TODO auto-generated method stub8 //Get all Java files in the IO directory9File dir =NewFile ("H:\\workspace\\io");Ten  Onelist<file> list = FileList (dir, ". Java");//Path list, incoming filter A  - //Gets a file that matches the condition in the path, writes a txt -File DestFile =NewFile ("H:\\workspace\\testfile\\javalist.txt"); the 
 -             Write2file (List,destfile);  +}
2. Filtering the file method, passing in the specifiedPath Parametersand filesuffix parameter,Returns the File list collection
1 2     /** 3 * Defines a collection that gets the specified filter condition 4 * @param dir path 5 * @param string suffix. java 6 * @re Turn 7 * /8  Public StaticList<file> fileList (File dir, String suffix) {9 //1. Defining a collectionTenlist<file> list =NewArraylist<file> (); One  A //2. Filters -FileFilter filter =NewFilefilterbysuffix (suffix); -Getfilelist (dir, list, filter); the  - returnList -  -}
3. Filter methods and Filter classes
1 /** 2 * Defines a collection that gets the specified filter condition 3 * Multilevel directory, passing list 4 * @param dir path 5 * @param lis T file set 6 * @param filter Filter 7 * /8  Public Static voidGetfilelist (File dir, list<file> List, FileFilter filter) {9file[] files = dir.listfiles ();Ten  One  for(File file:files) { A if(File.isdirectory ()) { -Getfilelist (file, list, filter);//Recursive -}Else{ the //Filter Files - if(filter.accept (file)) { -List.add (file);//Add -} +} -} +} A} at 
1  Public classFilefilterbysuffixImplementsFileFilter {2 3 PrivateString suffix;4 5  PublicFilefilterbysuffix (String suffix) {6 Super();7  This. suffix = suffix;8}9 Ten@Override One  Public BooleanAccept (File pathname) { A //TODO auto-generated method stub - returnPathname.getname (). endsWith (suffix); -} the  -}
4. Write list to file method
1 /** 2 * Write list to txt 3 * @param list file listing 4 * @param destfile Storage Object java file listing 5 */6 Private Static voidWrite2file (list<file> List, File destfile) {7 //TODO auto-generated method stub8BufferedWriter BUFW =NULL;9 Ten Try{ One //Use character buffer object BufferedWriter ABUFW =NewBufferedWriter (NewFileWriter (DestFile)); -  - //Traverse list, write absolute path the  for(File file:list) { -Bufw.write (File.getabsolutepath ());//write absolute path -Bufw.newline ();//Line break -Bufw.flush ();//Brush new record +} -  +}Catch(Exception e) { A //Todo:handle exception at}finally{ - if(BUFW! =NULL){ - Try{ -Bufw.close (); -}Catch(IOException e) { - //TODO auto-generated catch block in Throw NewRuntimeException (); -} to} +} -}

Java I/o---Get the file directory and write to text

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.