JAVA IO (file new, traverse, change, filter)

Source: Internet
Author: User

<!--new, traverse, change--

 public classIndex { public Static voidmain (string[] Args) {index ();        Test ();    FilePath (); }    Private Static voidFilePath () {//creates a file class relative path that defaults to the current projectFile File =NewFile ("src"); //get the path to all files (directories) in the current directoryfile[] files =File.listfiles ();  for(File f:files) {System.out.println (f); }    }    Private Static voidTest () {//creates a file class relative path that defaults to the current projectFile File =NewFile ("src/file operation/456/789/123"); //Create folder (directory)System.out.println (file.mkdirs ()); }    Private Static voidindex () {//creates a file class relative path that defaults to the current projectFile File =NewFile ("src/file operation/123.txt"); //determine the authenticity of a file (whether it exists)        BooleanIsfile =File.isfile (); //if it does not exist, create the current file        if(!Isfile) {            Try {                //Create a fileFile.createnewfile (); } Catch(ioexception e) {System.out.println ("file Creation failed"); }        } Else {            //file renameFile.renameto (NewFile ("src/file operation/123.chm")); }    }}

<!--file filtering--

 public classIndex { public Static voidmain (string[] Args) {File dir=NewFile ("src/new _ traverse _ change"); //get all the files (directories) under the directory and filter them,file[] fs = Dir.listfiles (Newfilenamefilter () {//implementation of filtering methods in the filter interface             public BooleanAccept (File dir, String Name) {return NewFile (dir, name). isfile () && name.endswith ("txt");        }        });  for(File F:fs) {System.out.println (f); }    }}

JAVA IO (file new, traverse, change, filter)

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.