PackageZl_file;ImportJava.io.File;Importjava.io.IOException;/*If we want to implement the IO operation, we must know the representation of the file on the hard disk. And Java provides a class file for us to use.File: Abstract representation of path names for files and directories (folders)Construction Method: These three effects are the same, but none of them really creates a folder or file (String pathname): Gets the File Object file (string parent, string child) according to a path: based on a directory and A sub-file/directory Gets the file object file (file parent, String child): Gets the file object based on a parent file object and a sub-file/directoryCreating features: If you forget to write the drive letter path, the default is under Project path. public boolean createnewfile (): Create file If such a file exists, the public boolean mkdir () is not created: Create folder if such a folder exists, public B is not created Oolean mkdirs (): Create a folder, if the parent folder does not exist, will help you to create it, if there is such a folder, do not create the note: Whether you want to create a file or a folder, make sure that the method is not adjusted wrong.Remove Features:p ublic Boolean Delete () Note: A: If you create a file or a folder that you forgot to write the drive path to, the default is under Project path. Delete in B:java does not go to recycle Bin. C: To delete a folder, please note that the folder cannot contain files or folders, or you cannot deleteRenaming feature:p ublic boolean Renameto (File dest) is renamed if the path name is the same. If the path name is different, it is renamed and clipped. Path begins with a drive letter: the absolute path c:\\a.txt path does not start with a drive letter: relative path a.txt*/ Public classFileDemo1 { Public Static voidMain (string[] args)throwsIOException {//file (String pathname): Gets the file object based on a path//encapsulate the H:\\demo\\a.txt as a file objectFile file1 =NewFile ("H:\\demo\\a.txt"); //file (string parent, String child): Gets the file object based on a directory and a sub-file/directoryFile file2 =NewFile ("H:\\demo", "A.txt"); //file (file parent, String child): Gets the file object based on a parent file object and a sub-file/directoryFile File3 =NewFile ("H:\\demo"); File file4=NewFile (File3, "A,txt"); //These three effects are the same, but none of them really creates a folder or file//Creating features//Public Boolean mkdir (): Create folder if such a folder exists, you do not create a//file File5 = new file ("H:\\demo\\a.txt");//Create error because there is no demo folder under H diskFile file5 =NewFile ("H:\\demo"); File5.mkdir (); //Public Boolean CreateNewFile (): Create file If such a file exists, you do not create aFile File6 =NewFile ("H:\\demo\\a.txt"); File6.createnewfile (); //Public Boolean mkdirs (): Create a folder, if the parent folder does not exist, will help you create it, if there is such a folder, do not create aFile File7 =NewFile ("H:\\test\\b.txt");//B.txt is also a folder, the name is arbitrary, or a folderFile7.mkdirs (); //Delete function: public boolean delete ()//to delete a folder, note that the folder cannot contain files or folders, or it cannot be deleted. So delete to delete from child to parentFile delete1 =NewFile ("H:\\test\\b.txt"); Delete1.delete (); File Delete2=NewFile ("H:\\test"); Delete2.delete (); //Rename function: Public boolean renameto (File dest)//if the path name is the same, it is renamed. //file rename1 = new file ("H:\\demo\\a.txt"); //file rename2 = new file ("H:\\demo\\b.txt"); //Rename1.renameto (rename2);//Change the a.txt of the H:\\demo path to B.txt.//if the path name is different, it is renamed and clipped. File Rename3 =NewFile ("H:\\demo\\b.txt"); File Rename4=NewFile ("H:\\a.txt"); Rename3.renameto (rename4);//paste the H:\\demo\\b.txt cut into the H-disk and rename it to A.txt }}
Some methods of the Java 19-12 file Class 1