The concept of file: it is the class file for our local file management
java can operate our local disk
file
route
Using the file class
Construction method:
File(File parent, String child)
Create a new File instance based on the parent abstract pathname and the child pathname string.
File(String pathname)
Create a new File instance by converting the given pathname string to an abstract pathname.
File(String parent, String child)
Create a new File instance based on the parent pathname string and the child pathname string.
//Abstract: it means that the directory represented by file may or may not exist
Get files or directories
String [] list
Member method:
String getAbsolutePath() returns the absolute pathname string of this abstract pathname. / / full path
String getName() returns the file or directory name represented by this abstract pathname / / itself
String getPath() converts this abstract path name to a path name string
length() the length of the file
boolean createNewFile() creates a file that does not exist
boolean exists() tests whether the file or directory represented by this abstract pathname exists
boolean isDirectory() determines whether it is a directory
boolean isFile() determines whether the relative path is a file
boolean delete() deletes the file or directory represented by this abstract pathname
boolean mkdir() creates a directory
boolean mkdirs() create directory
Traversal of directories or files in the File class
String s[] = file.list()
Returns an array of strings that specify the files and directories in the directory represented by this abstract pathname.
File f[] =file.listFiles()
Returns an array of abstract pathnames representing files in the directory represented by this abstract pathname.
File f[]=file.listFiles(FileFilter filter)
Returns an array of abstract pathnames that represent the files and directories in the directory represented by this abstract pathname that meet the specified filter
import java.io.File; public class demo2 { static int i =0; public static void main(String[] args) { // TODO Auto-generated method stub File f = new File("E:\\Program Files\\feiq\\Recv Files\\0730"); method(f); System.out.println(i); } public static void method(File f) { File [] files =f.listFiles(); for(File ff:files) { if(ff.isFile()) { if(ff.getName().endsWith(".mp4")) { System.out.println(ff); i++; } }else { method(ff); } } } }
import java.io.File; import java.io.FileFilter; public class demo03 { static int i =0; public static void main(String[] args) { // TODO Auto-generated method stub File f = new File("E:\\Program Files\\feiq\\Recv Files\\0730"); method(f); } public static void method(File f) { //Filter first File[] files =f.listFiles(new FileFilter() { //Anonymous inner classes implement methods in interfaces @Override public boolean accept(File pathname) { //Override the accept method in the interface // TODO Auto-generated method stub if(pathname.isFile()) { String name =pathname.getName(); return name.endsWith(".mp4"); }else { method(pathname); } return false; } }); for(File ff:files) { i++; if(ff.isDirectory()) { method(ff); }else { System.out.println(ff.getName()); } } } }
result:
01 operation mp4
02 usage scenario of anonymous objects mp4
03 usage scenario of anonymous objects mp4
04 overview of internal classes mp4
05 classification of internal classes mp4
06 initialization of inner class of member mp4
07 local internal class mp4
08 anonymous inner class mp4
09 observe from the generated class file mp4
10 real use of anonymous inner classes mp4
11. Detailed explanation of anonymous inner class polymorphism mp4
12 code block mp4
13 purpose of static code block mp4
14 package overview mp4
15 automatic packing mp4
16use of import mp4
17 enhanced for mp4
01 operation mp4
02 usage scenario of anonymous objects mp4
03 usage scenario of anonymous objects mp4
04 overview of internal classes mp4
05 classification of internal classes mp4
06 initialization of inner class of member mp4
07 local internal class mp4
08 anonymous inner class mp4
09 observe from the generated class file mp4
10 real use of anonymous inner classes mp4
11. Detailed explanation of anonymous inner class polymorphism mp4
12 code block mp4
13 purpose of static code block mp4
14 package overview mp4
15 automatic packing mp4
16use of import mp4
17 enhanced for mp4