The file class can operate on files in the operating system:
Static member variable for file class:
Packagedemo;ImportJava.io.File; Public classFiledemo { Public Static voidMain (string[] args) {//static methods for file classesString separator =File.pathseparator; SYSTEM.OUT.PRINTLN (separator); //output:; Directory separator, which is in Linux:Separator =File.separator; SYSTEM.OUT.PRINTLN (separator); //output: \ Directory name separator, Linux Yes/ }}
How to construct the file class:
Packagedemo;ImportJava.io.File; Public classFiledemo { Public Static voidMain (string[] args) {function1 (); Function2 (); Function3 (); } Public Static voidfunction1 () {File file=NewFile ("D:\\lol"); System.out.println (file); //Output: D:\lol } Public Static voidfunction2 () {File file=NewFile ("D:", "LOL"); System.out.println (file); //Output: D:\lol } Public Static voidFunction3 () {File parent=NewFile ("D:"); File File=NewFile (parent, "LOL"); System.out.println (file); //Output: D:\lol }}
The function of the file class:
Create and delete:
Packagedemo;ImportJava.io.File;Importjava.io.IOException; Public classFiledemo { Public Static voidMain (string[] args)throwsIOException {function1 (); Function2 (); Function3 (); } Public Static voidFunction1 ()throwsIOException {//creates a file, and if it exists, no longer createsFile File =NewFile ("D:\\lol\\java.txt"); Booleanb =File.createnewfile (); System.out.println (b); } Public Static voidFunction2 ()throwsIOException {//Create a single-level folderFile file1 =NewFile ("d:\\lol\\cs1.6"); BooleanB1 =File1.mkdir (); System.out.println (B1); //Create a multilevel folderFile file2 =NewFile ("D:\\gta\\gta5\\game"); BooleanB2 =File2.mkdirs (); System.out.println (B2); } Public Static voidFunction3 ()throwsIOException {//Delete , not go through Recycle Bin, delete directly from hard diskFile File =NewFile ("D:\\lol\\java.txt"); Booleanb =File.delete (); System.out.println (b); }}
Get Features:
Packagedemo;ImportJava.io.File; Public classFiledemo { Public Static voidMain (string[] args) {function1 (); Function2 (); Function3 (); Function4 (); } Public Static voidfunction1 () {File file=NewFile ("D:\\lol\\ League of Legends \\TCLS\\Client.exe"); String name= File.getname ();//gets the name of the last part of the pathSYSTEM.OUT.PRINTLN (name);//Client.exe } Public Static voidfunction2 () {File file=NewFile ("D:\\lol\\ League of Legends \\TCLS\\Client.exe"); LongLength = File.length ();//the number of bytes in the fileSystem.out.println (length);//813088 } Public Static voidFunction3 () {File file=NewFile ("D:\\lol\\ League of Legends \\TCLS\\Client.exe"); File Path= File.getabsolutefile ();//gets the absolute path of the fileSystem.out.println (path); //output: D:\lol\ League of Legends \tcls\client.exe } Public Static voidFunction4 () {File file=NewFile ("D:\\lol\\ League of Legends \\TCLS\\Client.exe"); File Parent= File.getparentfile ();//Get Parent PathSystem.out.println (parent); //output: D:\lol\ League of Legends \tcls }}
Judging function:
Packagedemo;ImportJava.io.File; Public classFiledemo { Public Static voidMain (string[] args) {function1 (); Function2 (); } Public Static voidfunction1 () {File file=NewFile ("D:\\lol\\ League of Legends \\cs1.6.exe"); Booleanb = file.exists ();//determine if a file (clip) existsSystem.out.println (b);//false } Public Static voidfunction2 () {File file=NewFile ("D:\\lol\\ League of Legends"); if(File.exists ()) {Booleanb = file.isdirectory ();//determine if it is a folder (path)System.out.println (b);//true } }}
Traverse directory Fetch (list fetch):
Packagedemo;ImportJava.io.File; Public classFiledemo { Public Static voidMain (string[] args) {function1 (); Function2 (); } Public Static voidfunction1 () {File file=NewFile ("D:\\lol\\ League of Legends"); String[] Strarr= File.list ();//get files and folders under the directory for(String Str:strarr) {System.out.println (str); } /*output: 7z.dll cross Game leagueclient soft_repair SpannedFileList.txt TCLS Tqm.ini League of Legends. LNK League of Legends Uninstall the. exe access official website. URL*/ } Public Static voidfunction2 () {//the same functionality, but it is recommended to use this//because it's more powerful, you can do more with it later.File File =NewFile ("D:\\lol\\ League of Legends"); File[] Filearr=File.listfiles (); for(File F:filearr) {System.out.println (f); } /*Output: D:\lol\ League of Legends \7z.dll D:\lol\ League of Legends \cross D:\lol\ League of Legends \game D:\lol\ League of Legends \leagueclient D:\lol\ League of Legends \soft_repair D:\lol\ League of Legends \spannedfilelist.txt D:\lol\ League of Legends \tcls D:\lol\ League of Legends \tqm.ini D:\lol\ League of Legends \ League of Legends. LNK d:\lol\ League of Legends \ League of Legends Uninstall. EXE d:\lol\ League of Legends \ Visit official website. URL*/ }}
Use recursion to traverse all the files in a directory:
Packagedemo;ImportJava.io.File;/** For all content under a directory, make a full traversal of the recursive call of the method, call yourself*/ Public classFiledemo { Public Static voidMain (string[] args) {File dir=NewFile ("D:\\lol\\ League of Legends"); Getalldir (dir); } /** Define methods to implement full traversal of the directory*/ Public Static voidGetalldir (File dir) {System.out.println (dir); //Call Method Listfiles () to traverse the directory, dirfile[] Filearr =Dir.listfiles (); for(File f:filearr) {//determines whether the path represented by the variable F is a folder if(F.isdirectory ()) {//is a directory, it is necessary to traverse this directory//continue calling Getalldir, passing his directoryGetalldir (f); } Else{System.out.println (f); } } }}
File filter:
When traversing a directory, you can only get the files that meet the criteria as needed
Packagedemo;ImportJava.io.File; Public classFiledemo { Public Static voidMain (string[] args) {File file=NewFile ("D:\\lol\\ League of Legends"); File[] Filearr= File.listfiles (Newmyfilter ()); for(File F:filearr) {System.out.println (f); } }}//Print only one: D:\lol\ League of Legends \ League of Legends Uninstall. exe
Package demo; Import Java.io.File; Import Java.io.FileFilter; Public class Implements FileFilter { publicboolean Accept (File pathname) { // is exe file returns True, otherwise returns falsereturn pathname.getname (). EndsWith (". exe");} }
Java Learning Note (file Class)