Java8--io Tool Class (Java Madness Handout 3 Review notes)

Source: Internet
Author: User

Paths class

 Public Static voidpathtest () {Path Path= Paths.Get("~"); System. out. println (path); System. out. println (Path.getnamecount ()); System. out. println (Path.getroot ()); Path Absolutepath=Path.toabsolutepath (); System. out. println (Absolutepath); System. out. println (Absolutepath.getnamecount ()); System. out. println (Absolutepath.getfilesystem ()); //Print Parent Path sequentiallyAbsolutepath.foreach (Name->system. out. println (name)); }

Files class, this is the point.

 Public Static voidFilestest () throws FileNotFoundException, ioexception{//Copying FilesFiles.copy (Paths.Get("Abc.txt"),NewFileOutputStream ("Testfile/abc2.txt")); //determine if the file is hiddenSystem. out. println (Files.ishidden (Paths.Get("Abc.txt"))); //read all rows of a file to listList<string> lines = Files.readalllines (Paths.Get("Abc.txt")); Lines.foreach ( LineSystem. out. println (line)); //File SizeSystem. out. println (Files.size (Paths.Get("Abc.txt"))); //write something to a filelist<string> poem =NewArraylist<string>(); Poem.add ("quzhinannnnnnnnnnn Chizhinan Nanan that that's that");        Poem.addall (lines); Files.write (Paths.Get("Testfile/path.txt"), poem, Charset.forname ("Utf-8")); //list all files under the file directory (not recursive)Files.list (Paths.Get("/"). ForEach (Name, System. out. println (Name)); //Print all files to the consoleFiles.lines (Paths.Get("Abc.txt"), Charset.forname ("Utf-8"). ForEach (Line-System. out. println (line)); //View storage spaceFilestore fs = Files.getfilestore (Paths.Get("/")); System. out. println (Fs.gettotalspace ()); }

Filevisitor traversing files and directories

 Public Static voidWalkfiletree () throws ioexception{Files.walkfiletree (Paths.Get("."),NewSimplefilevisitor<path>() {@Override Publicfilevisitresult previsitdirectory (Path dir, Basicfileattributes attrs) throws IOException {//TODO auto-generated Method Stub//return Super.previsitdirectory (dir, attrs); System. out. println ("being visited:"+ dir +"Catalogue"); returnfilevisitresult.continue; } @Override Publicfilevisitresult visitfile (Path file, Basicfileattributes attrs) throws IOException {//TODO auto-generated Method Stub//return super.visitfile (file, attrs); System. out. println ("\ t is accessing"+ file +"file"); if(File.endswith ("Learnio.java") ) {System. out. println ("****** Locate the target file learnio.java******"); returnFilevisitresult.terminate;//Stop when you find it.                }                returnFilevisitresult.continue;//I can't find it.            }          });    ; }

Monitor file changes with Watchservice

Private Static voidWatchservicetest () throws IOException, interruptedexception {//TODO auto-generated Method StubWatchservice Watchservice =Filesystems.getdefault (). Newwatchservice (); Paths.Get("."). Register (Watchservice, Standardwatcheventkinds.entry_create, STANDARDWATCHEVENTKINDS.E        Ntry_delete, standardwatcheventkinds.entry_modify);  while(true){            //Gets the change event for the next fileWatchkey key =Watchservice.take ();  for(watchevent<?>Event: Key.pollevents ()) {System. out. println (Event. Context () +"the file happened."+Event. Kind () +"Events"); }            //Reset WatchkeyBoolean valid =Key.reset (); //If reset fails, exit monitoring            if(!valid) {                 Break; }        }    }

Access to file properties, this feeling is not much, use to see it again.

Java8--io Tool Class (Java Madness Handout 3 Review notes)

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.