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)