File class: An abstract representation of the path names of files and directories.
Note: The file class can manipulate only the properties of the file, and the contents of the files cannot be manipulated.
1. Fields of the File class
We know that the path separators are not the same across platforms.
①, for UNIX platforms, the absolute pathname prefix is always "/" . There is no prefix for relative path names. The abstract path name that represents the root directory has a prefix "/" and an empty name sequence.
②, for Microsoft Windows platforms, the prefix that contains the path name of the drive specifier consists ":" of the drive letter followed, and may be followed if the path name is absolute "\\" . The UNC pathname is prefixed with a "\\\\" hostname and the share name is the first two names in the name sequence without a prefix for the relative path name of the specified drive.
So in order to block the delimiter differences between the various platforms, we can use the fields provided by Java as we construct the File class (see 2nd below).
System.out.println (file.separator);//output \ System.out.println (file.pathseparator);//output;
Then we can see:
File.pathseparator refers to separators that separate successive paths of strings
File.separator is used to separate directories in the same path string.
2. How to construct the File class
How to use the above construction method, see the following example:
Do not use the delimiter field provided by Java, Note: This write can only be valid on Windows platform file f1 = new file ("D:\\io\\a.txt"),//using Java provided delimiter file F2 = new file ("D:" +file . separator+ "IO" +file.separator+ "a.txt"); System.out.println (F1);//Output D:\IO\a.txt System.out.println (F2);//Output D:\io\a.txt//file (File parent, String Child )//Create a new file instance from the parent abstract path name and the subpath name string. File F3 = new File ("D:"); File F4 = new file (F3, "IO"); SYSTEM.OUT.PRINTLN (F4); D:\io//file (String pathname)//Creates a new File instance by converting the given pathname string to an abstract path name. File f5 = new file ("D:" +file.separator+ "IO" +file.separator+ "a.txt"); System.out.println (F5); D:\io\a.txt//file (string parent, String child)//Creates a new File instance from the parent pathname string and the subpath name string. File F6 = new file ("D:", "io\\a.txt"); System.out.println (f6); D:\IO\a.txt
3. Common methods of File class
①, creating methods
1.boolean CreateNewFile () does not exist return true existence returns false
2.boolean mkdir () Create a directory if the previous level directory does not exist, it will create a failure
3.boolean mkdirs () creates a multi-level directory that is automatically created if the previous directory does not exist
②, Delete method
1.boolean Delete () deletes a file or directory, and if it represents a directory, the directory must be empty to delete
2.boolean deleteonexit () file is deleted after use is complete
③, judging method
1.boolean CanExecute () Determine if the file is executable
2.boolean canRead () Determine if the file is readable
3.boolean canWrite () Determine if the file is writable
4.boolean exists () determine if a file or directory exists
5.boolean isdirectory () Determine if this path is a directory
6.boolean Isfile () to determine whether a file
7.boolean Ishidden () determines if the file is hidden
8.boolean Isabsolute () determines whether an absolute path file does not exist or can be judged
④, obtaining methods
1.String GetName () Gets the file or directory name represented by this path
2.String GetPath () converts this path name to a pathname string
3.String GetAbsolutePath () returns the absolute form of this abstract path name
4.String getParent ()//If no parent directory returns null
5.long lastmodified ()//Gets the last modified time
6.long () returns the length of the file represented by this abstract path name.
7.boolean Renameto (file f) Renames files represented by this abstract path name.
8.file[] Liseroots ()//Get Machine drive letter
9.string[] List () returns an array of strings that name the files and directories in the directory represented by this abstract path name.
10.string[] List (FilenameFilter filter) returns a string array that names the files and directories in the directory represented by this abstract pathname that satisfy the specified filter.
File (file parent, string child)//Creates a new file instance from the parent abstract path name and subpath name string. File dir = new file ("D:" +file.separator+ "IO"); File File = new file (dir, "a.txt");//Determine if Dir exists and represents a directory if (! Dir.exists () | | Dir.isdirectory ())) {//If Dir does not exist, create this directory Dir.mkdirs ();//Create A.txt file File.createnewfile () based on directory and file name;} Returns the name of the file or directory represented by this abstract path name. This is just the last name in the path name sequence. If the name sequence of the pathname is empty, an empty string is returned. System.out.println (File.getname ()); a.txt//returns the path name string for the parent null of this abstract pathname, or null if the pathname is not named parent. System.out.println (File.getparent ());//d:\io//converts this abstract pathname to a pathname string. The result string uses the default Name-separator character to separate the names in the name order. System.out.println (File.getpath ()); D:\IO\a.txt
4. Some techniques of File
①, print the contents of all folders and folders in a given directory
public static void Getfilelist (file file) {//first-level subdirectory file[] files = file.listfiles (); for (file f:files) {// Print directories and Files System.out.println (f); if (F.isdirectory ()) {getfilelist (f);}}}
Test:
public static void Main (string[] args) throws Exception {file F = new File ("D:" +file.separator+ "Webstormfile"); Getfilelis T (f);}
Java io Detailed (a)------File class