1 File objects
Public classCreateFile { Public Static void Main(string[] args) {//Create a file path and name to manipulate //Where file.separator represents a system-related delimiter, under Linux:/windows: \ \ //path represents the parent directory in this program, does not contain sub-filesString Path ="D:\\Program Files (x86) \\ADT\\workspace\\JavaIO\\parent";//childpath represents subdirectories within this program, including sub-filesString Childpath ="D:\\Program Files (x86) \\ADT\\workspace\\JavaIO\\demo.txt";//Constructs a file object in a separate way from parent directory and sub-file //can also be written as New File ("D:\\Program Files (x86) \\ADT\\workspace\\JavaIO\\parent", "test.txt");File F1 =NewFile (Path,"Test.txt");//Use absolute path to construct file object //can also be written as New File ("D:\\Program Files (x86) \\ADT\\workspace\\JavaIO\\demo.txt");File F2 =NewFile (Childpath);//Create a file object for parent directoryFile parent =NewFile (path);//Building a new file object with existing parent directory objects and sub-filesFile F3 =NewFile (Parent,"Hello.txt"); System. out. println ("The path of F1 ="+ F1); System. out. println ("The path of F2 ="+ F2); System. out. println ("The Path of F3 ="+ F3); }}
Result diagram
It's worth noting that the code just created the object and didn't generate the file!!
After running the code, there are no folders and files that generate a response under that folder.
2 Creating files and deleting files
Public classCreatefileanddelete { Public Static void Main(string[] args) {String Path ="D:\\Program Files (x86) \\ADT\\workspace\\JavaIO\\parent"; File f =NewFile (Path);Try{/* Because creating and deleting files involves underlying operations, it is possible to throw an exception */ //Returns TRUE if the creation is successful //If the file already exists, the creation is unsuccessful, return flase, do not think it will overwriteSystem. out. println ("Create file:"+ F.createnewfile ());//delete file, return true successfully, otherwise return flaseSystem. out. println ("Delete file:"+ F.delete ());//This method means that the file is deleted when the virtual machine exits //Reason: The program is running with the possibility of an exception resulting in a direct exit //cleaning residue is necessary ~! F.deleteonexit (); }Catch(IOException e) {E.printstacktrace (); } }}
Results
3 File Judgments and tests
Public classfiletest { Public Static void Main(string[] args) {String Path ="D:\\Program Files (x86) \\ADT\\workspace\\JavaIO\\parent"; File f =NewFile (Path);//Determine if the file can be executedSystem. out. println ("F is executable:"+ F.canexecute ());//Determine if the file existsSystem. out. println ("F is present:"+ f.exists ());//Determine if the file is readableSystem. out. println ("F is readable:"+ F.canread ());//Determine if the file is writableSystem. out. println ("F can be written:"+ F.canwrite ());//Determine if the file is an absolute path nameSystem. out. println ("F is absolute path:"+ F.isabsolute ());//Determine if the file is a standard fileSystem. out. println ("F is a standard file:"+ F.isfile ());//Determine if the file is a directorySystem. out. println ("F is directory:"+ f.isdirectory ());//Determine if the file is hiddenSystem. out. println ("F is hidden:"+ F.ishidden ()); } }
Results if the parent folder does not exist
Results in the presence of the parent folder
4 Creating a Folder
public Class Createfilepath {public static Span class= "Hljs-keyword" >void main (string[] args) {String path = "D:\\Program Files (x86) \\ADT\\workspace\\JavaIO\\parent" ; //path here as the parent directory exists File F1 = new File (path, "/ABC" ); File F2 = new File (path, "/d/e/f/g" ); //Create a directory System. out . println (F1.mkdir ()); //recursively create a directory System. out . println (F2.mkdirs ()); } }
Result diagram before running code
Result diagram after running code
Console output is
5 Getting file information
Public classGetFileInfo { Public Static void Main(string[] args) {String Path ="D:\\Program Files (x86) \\ADT\\workspace\\JavaIO\\parent"; File f =NewFile (path);//Returns the absolute path of the file //return value here is StringSystem. out. println ("F's Absolute path name:"+ F.getabsolutepath ());//Returns the absolute path of the file //Return value here is fileSystem. out. println ("F's Absolute Path object:"+ F.getabsolutefile ());//Returns the name of the file or directorySystem. out. println ("F's name:"+ F.getname ());//Returns the relative path of the file ////What path is encapsulated in the constructor and what path is returnedSystem. out. println ("F's Path:"+ F.getpath ());//Returns the path to the parent directory //If the path in the constructor is not an absolute path, NULL is returned hereSystem. out. println ("F's parent directory:"+ f.getparent ()); }}
Result diagram
Console output Information:
6 listing the root directory of the file system
publicclass GetRootPath { publicstaticvoidmain(String[] args) { //listRoots()是一个静态方法,返回文件数组 File[] files = File.listRoots(); //foreach循环打印File对象 for (File x : files) { System.out.println(x); } }}
Results
7 List all files and directories under the file
Public classGetfilechildfile { Public Static void Main(string[] args) {String Path ="D:\\Program Files (x86) \\ADT\\workspace\\JavaIO\\parent"; File f =NewFile (path);//Mode one: List () //Returns an array of strings containing all filenames under the specified directory //If it is not a directory, returns nullstring[] files = f.list (); for(String x:files) {System. out. println (x); }//Way Two: Listfiles () //Returns the file array /* file[] files = f.listfiles (); for (File x:files) {//If you need to include a path, print x directly System.out.println (X.getname ()); } */}}
Result diagram:
8 recursively lists all files in the directory
Public classGetallchilefile { Public Static void Main(string[] args) {String Path ="D:\\Program Files (x86) \\ADT\\workspace\\JavaIO\\parent"; File f =NewFile (path);//Call the following recursive methodPrint (f); }//Print a list of directories in a recursive manner Public Static void Print(File f) {if(F.isdirectory ()) {file[] files = f.listfiles (); for(File x:files) {print (x); } }Else{System. out. println (f); } }}
Results
If you have any questions or errors, please leave a message! Thanks, ~~!.
The source code is given later to download
Java io Flow details (ii)