Today I'm going to summarize the Java file class. This is a very important class.
The first is the mind map I drew.
Also wrote some of their own written code.
/** * DATE:2017/6/24 * AUTHOR:HSJ * Description: */public class Demo {/** * file (pathname) indicates the path of the files or folders * File (String parent,child); * File (file parent,child); */@Test public void Fun () {//The first constructor method file File = new file ("f:\\baiduyundownload\\01, first stage Java Basics (more video teaches Cheng Www.pcsky.wang) \\day19\\day19\\video\\001_ today content. avi "); System.out.println ("File path exists:" + file.exists ()); File File2 = new file ("Ddd.txt");//project Folder System.out.println (File2.exists ()); The second construction method is the String parent = "f:\\baiduyundownload\\01, First stage Java Foundation (more video tutorial Www.pcsky.wang) \\day19\\day19\\video"; String child = "001_ today content. avi"; File File3 = new file (parent, child); System.out.println (File3.exists ()); The third constructor, file File4 = new file ("f:\\baiduyundownload\\01, first-stage Java Basics (more video tutorial Www.pcsky.wang) \\day19\\day19\\video"); Encapsulated as file String child2 = "001_ today content. avi"; File File5 = new file (file4, child2); System.out. println (File5.exists ()); }/** * Create file * CreateNewFile ();//Create folder, there is create return true, do not put back false * mkdir (); Create folder * mkdirs () Create folder and parent folder * /@Test public void fun2 () {/* File File = new file ("Yyy.txt"); try {Boolean newFile = File.createnewfile (); System.out.println (NewFile); } catch (IOException e) {e.printstacktrace (); }*//* File File2 = new file ("make"); Boolean mkdir = File2.mkdir (); System.out.println (mkdir); */File File3 = new file ("make//make//m"); Boolean mkdirs = File3.mkdirs (); System.out.println (mkdirs); }/** * Renameto () Rename or cut * Delete () delete files or folders * Folders cannot contain folders or files * Folders cannot contain folders or files * * @Test PU Blic void Fun3 () {/* File File = new file ("Make//make/m/xx.txt"); File File2 = new file ("Ddd.txt"); Boolean b = File2.renameto (file);//Finish renaming the file or specify the drive letter cut System.out.println (b); *//* file File3=new FiLe ("yyy.txt"); File3.delete (); */File File4 = new file ("Make//make//m/xx.txt"); Boolean delete = File4.delete (); SYSTEM.OUT.PRINTLN (delete); }/** * Judgment function */@Test public void Fun4 () {File File = new file ("Zzz.txt"); /* File.setwritable (FALSE);//Set not writable System.out.println (File.canwrite ()); *//* file.setexecutable (FALSE); */ /* try {file.createnewfile (); Create file} catch (IOException e) {e.printstacktrace (); }*/}/** * Get function * public string GetAbsolutePath (): Get Absolute path * public string GetPath (): Get path * public St Ring GetName (): Gets the name * public long Length (): Gets the length. Bytes * Public long lastmodified (): Gets the last modified time, millisecond value * Public string[] List (): Gets an array of names of all files or folders under the specified directory * public file [] Listfiles (): Gets the file array of all files or folders under the specified directory */@Test public void Fun5 () {/* file File=new file ("Zzz.txt"); System.out.println (File.getabsolutefile ());///Get absolute path System.out.println (File.getparent ());//can only be used for paths, get parent path System.out.println (File.getpath ());//Get the path that the constructor passed over System.out.println (File.getname ());//Get File name System.out.println (File.length ()); System.out.println (File.lastmodified ());//Gets the last modified time millisecond value Date d=new date (file.lastmodified ()); SimpleDateFormat sdf=new SimpleDateFormat ("yyyy mm month DD Day HH:mm:ss"); System.out.println (Sdf.format (d)); */* File File=new file ("C:\\users\\abc\\desktop\\javawork\\struts2\\struts2mav EnDemo3 "); String [] filelist=file.list (); for (String s:filelist) {System.out.println (s); } System.out.println (File.getabsolutefile ()); */} public static void Issuffixjpg (file file, LIST<STRING&G T mlist) {file[] fileList = File.listfiles ();//Gets the file if (fileList! = null) {for (File F:fileli ST) {if (F.isdirectory ()) {issuffixjpg (f, mlist); } else {String fileName = F.getname (); if (Filename.endswith (". jpg")) Mlist.add (F.getname ()); }}}} @Test public void Fun6 () {File File = new file ("f:\\ my file \ \ My picture \\TU"); list<string> list = new arraylist<> (); Issuffixjpg (file, list); System.out.println (List.size ()); for (String s:list) {System.out.println (s); }}//traverse the file under the specified folder @Test public void Fun7 () {File File = new file ("f://"); string[] SList = file.list (new FilenameFilter () {@Override public boolean accept (file dir, String n AME) {System.out.println (Dir.getname ()); System.out.println (); return true; } }); for (String s:slist) {System.out.println (s); }} @Test public void Fun8 () {list<string> Slist=neW arraylist<> (); File File=new file ("f:\\ my files \ \ My Pictures \\TU"); Issuffix (file,slist); for (String s:slist) {System.out.println (s); }} public static void Issuffix (File file,list<string> mlist) {file[] files = file.listfiles (); for (File f:files) {if (F.isdirectory ()) {issuffix (f,mlist); } else {String name = F.getname (); if (Name.endswith (". jpg")) {Mlist.add (name); }}}} @Test public void Fun9 () {File file= new file ("d://"); string [] slist=file.list (New FilenameFilter () {@Override public boolean accept (file dir, String na Me) {/*system.out.println (dir);//Package directory D-Disk System.out.println (name);//directory name of the file */ File File2=new file (dir,name); Return File2.isfile () &&file2.getnamE (). EndsWith (". jpg"); } }); for (String s:slist) {System.out.println (s); }} @Test public void Fun10 () {File File=new file ("d://"); file [] Files=file.listfiles (New FileFilter () {@Override public boolean accept (file pathname) { return true; } }); for (File f:files) {System.out.println (F.getname ()); } }}
Java File class