Java. Io. File Operations
The file API provides us with a general abstract file used to operate files and directories on computers. It adopts the Unix idea and is also considered as a file, so all are processed using the file class. When creating a file class instance, its initialization value means to load the corresponding file (or directory) to be processed, such as: file objdir = new file ("D:/XP/test "); file objfile = new file ("D:/XP/test/filename.txt"); Note: You can use/to represent directories in a unified manner. This is true for UNIX systems, in Windows, you can use the following two instances to create directories and files. objdir. mkdir (); returns a Boolean indicating whether the creation is successful or not. You can only create (subdirectory) in an existing Directory. 2. objdir. mkdirs (); returns a Boolean indicating whether the creation is successful or not, even if the parent directory does not exist. For example, if file objdir = new file ("hehe/DIR"); indicates that two levels of directory hehe/DIR are created under the current directory, then mkdirs () is used, the mkdir () method fails. Note: If the new file ("/hehe/DIR") is created from the root directory of the current drive letter (d:/), because this root directory always exists, mkdir () methods can also be created successfully, but they are often not what we want. After creating a directory, you can create a file. If you create a file in a directory that does not exist, an exception is thrown. Objfile. createnewfile (); returns a Boolean indicating whether the creation is successful or not, so that an empty file is created: D: under/XP/test/filename.txt, you can view the file name, size, path, parent directory, readable and writable state, determine whether the file exists, whether it is a directory or file, and obtain the last modification time of the file., modify the file name, delete files or directories. Method Name: system. out. println ("absolute path:" + objfile. getabsolutepath (); system. out. println ("relative path:" + objfile. getpath (); system. out. println ("parent directory:" + objfile. getparent (); system. out. println ("readable:" + objfile. canread (); system. out. println ("writable:" + objfile. canwrite (); system. out. println ("file length:" + objfile. length (); // use the directory where the file is located to retrieve the list in it, and then process the system through a loop. out. println ("directory file list:" + (INT) (objdir. list (). length); system. out. println ("file name:" + objfile. getname (); system. out. println ("file existence no:" + objfile. exists (); system. out. println ("File deleted successfully:" + objfile. delete (); Note: To delete a directory objdir. delete (); the directory must be empty. If the directory is not empty, you can use objdir. list () gets the list of objects in the current directory string [], deletes the objects in the directory cyclically, and then deletes the directory.