------------------------method. Tool----------------------------
Get File path and detection status :
To manipulate the file path and name:
File getabsolutefile (): Gets the absolute path
String GetAbsolutePath (): Gets the absolute path
String GetPath (): Get File path
String getName (): Gets the file name
File Getparentfile (): Get parent Directory file
String getParent (): Gets the parent directory path
Methods for detecting the file state:
Boolean CanExecute (): Determines whether an executable file
Boolean CanRead (): Determines whether the file is readable
Boolean CanWrite (): Determines whether the file is writable
Boolean Ishidden (): Determines whether the file is a hidden file
Long LastModified (): Determines the last modified time of the file
Long Length (): Gets the length of the file (in units of bytes)
File method in Class- file operation:
Boolean Isfile (): Whether it is a file
Boolean CreateNewFile (): Creates a new file (this method returns true if the specified file does not exist and has been created successfully.) If the file exists, the method returns false. )
Static file Createtempfile (string prefix, string suffix): Creating temporary files
Boolean Delete (): Delete file
void Deleteonexit (): Deleting files when the JVM is stopped
Boolean exists (): Determine if the file exists
Boolean Renameto (File dest): Re-modify name
---------------------------------------------------------------------
File method in Class- Directory Operations
Boolean isdirectory (): Determines whether the directory
Boolean mkdir (): Create current directory
Boolean mkdirs (): Create current directory and parent directory
Static file[] Listroots (): List system drive letter
String[] List (): Lists all the file names
File[] Listfiles (): List all file objects
The list () method returns the file names of all files and directories under a directory, and returns a string array
The Listfiles () method returns the absolute path to all files and directories in a directory, and returns a file array
------------------------Case----------------------------
File1
1 //Case 1: Print files and directories from a directory on the disk to see2 //File File = new File ("e:\\javaall\\01javase\\ class note");3File File =NewFile ("e:/javaall/01javase/class notes");4 5 if(File.exists ()) {//Determine if a folder (directory) exists6string[] Lists =file.list ();7 for(String string:lists) {8System.out.println ("string =" +string);9 }Ten One}Else{ ASystem.out.println ("The folder ' directory ' does not exist"); -}
File2
1 //Case 2: Based on Case 1, make a description of the directory (folder) and the file separately2 Public Static voidMain (string[] args) {3File File =NewFile ("e:/javaall/01javase/class notes");4 if(File.exists ()) {5file[] Files =file.listfiles ();6 for(File file2:files) {7 if(File2.isfile ()) {//determine if it is a file8System.out.println (File2.getname () + "file" +file2.length () + "byte");9}Else{TenSystem.out.println (File2.getname () + "directory"); One } A } - the}Else{ -SYSTEM.OUT.PRINTLN ("Directory does not exist"); - } -}
File3
1 Public classFileDemo03 {2 //Case 3: Based on the above case, you want to be able to traverse a directory until there are no folders in the entire directory.3 4 Public Static voidshowfiles (file file) {5 if(File.isfile ()) {//file6System.out.println (File.getabsolutepath () + "," +7File.getname () + ", Size:" +file.length () + ", Last modified:" +8 NewSimpleDateFormat ("yyyy mm month DD Day HH:MM:SS").9FormatNewDate (file.lastmodified () ));Ten}Else{//folder Onefile[] Listfiles = File.listfiles ();//Subdirectories and Files A for(File file2:listfiles) { -Showfiles (file2);//themselves calling themselves recursively - } the } - } - - + Public Static voidMain (string[] args) { -File File =NewFile ("e:/javaall/01javase/class notes"); + if(File.exists ()) { A showfiles (file); at } - - - } - -}
Java Io-file Class