the role of the file class
1. To encapsulate a file or folder as an object
2. Easy to operate files and folders
3. Easy file transfer for input and output streams
Create a file object and encapsulate the existing and non-appearing files or folders as objects.
All four of these methods encapsulate the 1.Java file under the C drive as an object.
Second, the judgment of the file object
You can create an existing file object, or you can create a file object that does not exist.
Package Com.neuedu.demo01;import Java.io.File;/** Use of file classes*/ Public classFiledemo { Public Static voidMain (string[] args) {File F1=NewFile ("E:\\learnjava\\hello.txt"); System. out. println (F1.exists ());//determine if a file or directory existsSystem. out. println (F1.isdirectory ());//determines whether a file object represents a directorySystem. out. println (F1.isfile ());//determines whether a file object represents a fileSystem. out. println (F1.isabsolute ());//determine if the file object is an absolute pathSystem. out. println (F1.ishidden ());//determine if a file object is a hidden file }}Iii. creation and deletion of files
The file class provides methods to be able to common and delete files or directories on a computer
Package com.neuedu.demo01;import Java.io.file;import java.io.IOException;/** Use of file classes **/ Public classFiledemo { Public Static voidMain (string[] args) {File F1=NewFile ("E:\\learnjava\\hello.txt");//a fileFile f2=NewFile ("E:\\learnjava\\hello");//a directory if(F1.exists ()) {//file file already existsF1.delete ();//Delete the file that already exists}Else { Try{f1.createnewfile (); //If it does not exist, it is created}Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } } if(!f2.exists ()) {//If the directory does not existF2.mkdir ();//Create directory F2.mkdirs (); Create a multilevel catalog}Else{f2.delete (); } }}Iv. file-related get methods
Package com.neuedu.demo01;import Java.io.file;import java.io.IOException;/** Use of file classes **/ Public classFiledemo { Public Static voidMain (string[] args) {File F1=NewFile ("E:\\learnjava\\hello.txt");//a fileFile f2=NewFile ("E:\\learnjava\\hello");//a directorySystem. out. println (F1.getabsolutepath ());//gets the absolute path of the file object, which is returned----E:\learnJava\hello.txtSystem. out. println (F1.getabsolutefile ());//gets the absolute path of the file object and returns a string-----E:\learnJava\hello.txtSystem. out. println (F1.getname ());//gets the filename of the file object or the directory name----hello.txtSystem. out. println (F1.getparent ());//gets the parent directory of the file object and returns a string----E:\learnJavaSystem. out. println (F1.getparentfile ());//gets the file object's parent directory file object, which is returned----E:\learnJava Try{System. out. println (F1.getcanonicalpath ());//gets the canonical path name of the file object----E:\learnJava\hello.txtSystem. out. println (F1.getabsolutefile ());//gets the canonical path form of the file object, returning the Document object----E:\learnJava\hello.txt}Catch(IOException e) {//TODO auto-generated Catch blockE.printstacktrace (); } System. out. println (F1.getpath ());///get file path E:\learnJava\hello.txt }}V. Directory of recursive files
Returns the files and directories in the file directory via list (), and returns a string
Returns files and directories in the file directory by ListFile (), and returns a file object
Package com.neuedu.fileutils;import Java.io.file;import java.io.IOException;/** * * * @author WZC * lists some common actions for file*/ Public classFileUtils {/** * List all files under the specified directory and sub-directories * @param dir * @throws IOException*/ Public Static voidlistdirectory (File dir) throws ioexception{//determine if a directory exists if(!dir.exists ()) { Throw NewIllegalArgumentException ("Catalogue"+dir+"does not exist"); } //Determine if File dir is a directory if(!dir.isdirectory ()) { Throw NewIllegalArgumentException (dir+"not a directory"); } /*string[] list = Dir.list (); Lists the files or directories in the file, excluding the for (string string:list) {System.out.println (string) under subdirectories; }*/ //If you need to traverse the contents of a subdirectory, you need to construct the object to continue the recursive sfile[] files = dir.listfiles ();//returns a direct subdirectory object /*For (file file2:files) {System.out.println (file); }*/ if(files!=NULL&& files.length>0) { for(File file:files) {if(File.isdirectory ()) {//if the sub-file is a directory//Recursivelistdirectory (file); }Else{System. out. println (file);//If the file is the output } } } }}
Java File class