Java.io.File class
- All classes and interfaces related to input and output are defined under the Java.io package.
- File is a class that can have constructors to create its objects. This object corresponds to a file (. txt. avi. doc. ppt. mp3. jpg) or a file directory
- The file class object is platform-independent
- The method in file only deals with how to create, delete, rename, and so on. File is powerless and must be completed by IO Stream as long as it involves the contents of the document.
- The object of the file class is often the parameter of the constructor of the concrete class of the IO stream
One, CreateNewFile () Delete () LastModified () exists ()
PackageCom.yyx.pratice;ImportJava.io.File;Importjava.io.IOException;Importjava.util.Date; Public classJavapratice { Public Static voidMain (string[] args) {String FilePath= "D:" + file.separator + "testfile.txt"; System.out.println (FilePath); File File=NewFile (FilePath); //determine if a file exists and is not a file if(File.isfile () &&file.exists ()) { //Deleting FilesFile.delete (); } Else { Try { //Create a fileFile.createnewfile (); //print file Last edit timeSystem.out.println (NewDate (File.lastmodified ())); } Catch(IOException e) {e.printstacktrace (); } } }}
II, MkDir () mkdirs () list () Listfiles ()
PackageCom.yyx.pratice;ImportJava.io.File;Importjava.io.IOException; Public classJavapratice { Public Static voidMain (string[] args) {/** MkDir (): Create a file directory. Returns true only if the upper-level file directory exists
Mkdirs (): Creates a file directory. If the upper-level file directory does not exist, create*/String Mkdirpath= "D:" + file.separator + "Testfile"; File Mkdirfile=NewFile (Mkdirpath); if(!mkdirfile.exists ()) {Mkdirfile.mkdir (); } String mkdirsPath1= "D:" + file.separator + "Testfile" + file.separator + "File1"; File MkdirsFile1=NewFile (MKDIRSPATH1); if(!mkdirsfile1.exists ()) {Mkdirsfile1.mkdir (); } String mkdirsPath2= "D:" + file.separator + "Testfile" + file.separator + "File2" + file.separator + "File3"; File MkdirsFile2=NewFile (mkdirsPath2); if(!mkdirsfile2.exists ()) {Mkdirsfile2.mkdirs (); } /** Only the parent directory exists to create files in this directory*/String filePath1= "D:" + file.separator + "Testfile" + file.separator + "Filepath.txt"; File file1=NewFile (FILEPATH1); if(!file1.exists ()) { Try{file1.createnewfile (); } Catch(IOException e) {e.printstacktrace (); }} String filePath2= "D:" + file.separator + "Testfile" + file.separator + "File1" +File.separator+ "Filepath1.txt"; File file2=NewFile (filePath2); if(!file2.exists ()) { Try{file2.createnewfile (); } Catch(IOException e) {e.printstacktrace (); } } /** List () returns an array of strings that specify files and directories in the directory represented by this abstract pathname * Listfiles () returns an abstract path to the an array group that represents the files in the directory represented by this abstract pathname * You can use recursive loops to remove files and directories under the directory*/File File3=NewFile (Mkdirpath); String[] STRs=file3.list (); for(inti = 0; i < strs.length; i++) {System.out.println (strs[i]); } System.out.println ("*******************"); file[] Files=File3.listfiles (); for(inti = 0; i < files.length; i++) {System.out.println (Files[i].getname ()); } }}
Java file class files detailed