First, the file class;
The file class is the only object in the IO package that represents the disk file itself. The file class defines a platform-independent way to manipulate files by invoking methods in the file class to create, delete, rename, and so on.
The object of the file class is primarily used to obtain some information about the text, such as the directory where the file resides, the length of the file, the Read permission for the file, and so on.
1. Creation of files, creation of directories (folders)
File (String pathname): Fill in the file's constructor with a String-type path name, as follows:
String s= "D:\\aaa.txt";
File F=new file (s); or file F=new file ("D:\\aaa.txt");
(The above is the creation of the file (or directory) object, the object is created, it is necessary to start creating files (or directories).)
F.createnewfile (); (Create file) f.mkdir (Create directory)
(So the file is created,)
2. Deletion of files and directories;
As in the same way, first create the file object, call the Delete method of the object
F.delete ();
PackageCom.inba.maya.liu;ImportJava.io.File; Public classText { Public Static voidMain (string[] args)throwsexception{//1. Create a file /*String filename= "G:\\aaa.txt"; File F=new file (fileName); F.createnewfile (); This method builds the file*/ //2. Create a directory /*String dirname= "G:\\BBB"; File F=new file (dirName); F.mkdir (); This method builds the folder*/ //3. Delete--deleting files or directoriesString filename= "G:\\aaa"; File F=NewFile (fileName); F.delete (); }}
3. Renaming and moving
PackageCom.inba.maya.liu;ImportJava.io.File; Public classText { Public Static voidMain (string[] args)throwsexception{//third, rename or moveString n1= "F:\\bbb.txt"; String N2= "G:\\bbb.txt"; File F1=NewFile (N1); File F2=NewFile (n2); F1.renameto (F2); //is actually moved first, then renamed }}
4. List-Find the file or sub-file under this folder:
PackageCom.inba.maya.liu;ImportJava.io.File; Public classText { Public Static voidMain (string[] args)throwsexception{/*//The first way to traverse. List String dirname= "f:\\ re-engraved"; File F=new file (dirName); String[] Names=f.list ();//Out is an array of file objects for (int i=0;i<names.length;i++) {System.out.println (names[i]); /get file name, without path}*/ //The second way to traverse file[]String dirname= "f:\\ \ \ New folder \ \"; File F=NewFile (dirName); file[] Files=F.listfiles (); for(inti=0;i<files.length;i++){ //String s=files[i].getname ();//Get file name//String S=files[i].getpath ();//get file path, do not write get, default is GetPath//System.out.println (s);File dest=NewFile (dirname+i+ "HHH.txt"); Files[i].renameto (dest); } }}
In the second traversal mode, the Renameto method is used to modify the file name in bulk.
1. First define the source directory:String dirname= "f:\\ \ \ New folder \ \"
2.f.listfiles (), take out an array, pay file[]
3. Traverse, do not cycle once create a target file: file dest=new files (dirname+i+ "HHH.txt");
4.files[i].renameto (dest); Move the current file and rename it to the destination file.
5. Determine if it is a file or directory or hidden file, etc. (see the JDK document file class for details)
PackageCom.inba.maya.liu;ImportJava.io.*; Public classText1 { Public Static voidMain (string[] args)throwsException {String s= "C:"; File F=NewFile (s); file[] Files=F.listfiles (); for(inti=0;i<files.length;i++){ if(Files[i].isdirectory ()) {System.out.print ("Directory" "); }Else if(Files[i].ishidden ()) {System.out.print ("Hide File" "); }Else if(Files[i].isfile ()) {System.out.print ("File" "); } System.out.println (Files[i]); } }}
File class explanation