Java.io.File class, which is Java gets all the properties of a file/folder, and the class that completes all related operations
Example:
1 PackageTest.file.IO;2 3 ImportJava.io.*;4 Public classtestfile{5 Public Static voidMain (string[] args) {6 //The 1.File class is the class that Java uses to process files, and it can get all the properties of a file and all the operations required to complete the file7 //so the first step is to use a new file object8 //file F = new file ("Test.txt");//construction methods also have multiple overloads9 //2. Determine if the file exists, the exists () method, which is for a single file, so use the third person singularTen //3. If it does not exist, then create this file, CreateNewFile () One //If you are creating a folder, use MkDir (); You can create only one layer of folders and A //mkdirs () it is recommended to use this method regardless of how many layers are created; - //4. Determine if the file is a file or a folder, Isfile (); Isdirectory () - //5. If it is a file, get the basic properties of the file, GetName () get the filename; the //GetPath () gets the path; - //GetAbsolutePath () gets the absolute path; - //length () Get file size and so on - //6. Get the file's parent directory getParent () + //getparentfile () can get parent path - //7. If it is a folder, the main thing is to get the files in the directory + //list () returns the file name of all subdirectories, the return value is of type string (hidden file will also return) A //ListFile () It is clear that all files under all subdirectories are returned, and the type is file type at // - - -File f =NewFile ("D:\\123\\789\\abc.txt"); - if(F.exists ()) {//first determine if the file exists -System.out.println ("already exists"); in}Else{ - if(F.isdirectory ()) {//determine whether you want to generate a file or a folder toF.mkdirs ();//looping through the generation of folders and their subfolders +}Else { - Try { the if(!f.getparentfile (). exists ()) {//If you are creating a file that contains folders, you must first create the folders manually * f.getparentfile (). Mkdirs (); $ }Panax Notoginseng f.createnewfile (); -}Catch(Exception e) { the e.printstacktrace (); + } A } the } + - //When you are a folder, you can display its subdirectories and files $File F2 =NewFile ("e:\\tommy_130923"); $string[] FileNames = F2.list ();//list will list the files and subdirectories. - for(inti=0;i<filenames.length;i++) -System.out.println (i+ "" +filenames[i]); the -System.out.println ("***********************************");Wuyifile[] files = f2.listfiles ();//returns all files and subdirectories in a file format the for(intj=0;j<files.length;j++) -System.out.println (j+ "" +files[j].getname ()); Wu - //Looping through Files AboutSystem.out.println ("************** loops through file *********************"); $ showfiles (F2); - } - - Public Static voidShowfiles (File f) { A if(F.exists () &&f.isdirectory ()) { +file[] Files =f.listfiles (); the for(File file:files) { - if(File.isdirectory ()) $ showfiles (file); the Else the System.out.println (File.getabsolutepath ()); the } the -}Else if(F.exists () &&f.isfile ()) in System.out.println (F.getabsolutepath ()); the } the}View Code
Java file operation class files