The Java file class is very powerful, and Java can basically do all the work with the file. The text provides a detailed analysis of the classes of Java file Operations, and introduces common methods in the file class.
constructor function
public class Filedemo {public static void main (string[] args) {//constructor file (String pathname) file F1 = new file ("D:\\a\\1.txt //file (String parent,string child) file F2 = new File ("D:\\a", "2.txt");//file (file parent,string child) file F3 = new Fi Le ("D:" +file.separator+ "a"); File F4 = new file (F3, "3.txt"); System.out.println (F1);}}
Create a method
1 Boolean CreateNewFile () does not exist to return true; False2 Boolean mkdir () exists to Create directory 3 Boolean mkdirs () Create multilevel directory
Delete method
1 Boolean Delete ()
2 Boolean deleteonexit () file deleted after use
public class Filedemo {public static void main (string[] args) {file F = new File ("D:\\1.txt"); try {System.out.println (f.cr Eatenewfile ()); System.out.println (F.delete ());} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}
Judging method
1 Boolean CanExecute () determines whether the file can be executed
2 Boolean CanRead () to determine if the file is readable
3 Boolean CanWrite () to determine if the file is writable
4 Boolean exist () to determine if a file exists
5 Boolean isdirectory () to determine if the file is a directory
6 Boolean isfile () determines whether the file is
7 Boolean Canhidden () to determine if the file is visible
8 Boolean Isabsolute () to determine whether the file is absolute, the file does not exist can also be judged
Get method
1.String GetName ()
2.String GetPath ()
3.String GetAbsolutePath ()
4.String getParent ()//If no parent directory returns null
5.long lastmodified ()//Gets the last modified time
6.long Length ()
7.boolean Renameto (File f)
8.file[] Liseroots ()//Get Machine drive letter
9.string[] List ()
10.string[] List (filenamefilter filter)
List the files and folders under the disk
public class FileDemo3 {public static void Main (string[] args) { file[] files =file.listroots (); for (file file:files) { System.out.println (file); if (File.length () >0) { string[] filenames =file.list (); for (String filename:filenames) { System.out.println (filename);}}}}
File filtering
public class FileDemo4 {public static void Main (string[] args) { file[] files =file.listroots (); for (file file:files) { System.out.println (file); if (File.length () >0) { string[] filenames =file.list (new FilenameFilter () { //file filter directory name file name public Boolean Accept (File file,string filename) { return Filename.endswith (". mp3"); } ); for (String filename:filenames) { System.out.println (filename);}}}}
List all files recursively with recursion
public class FileDemo5 {public static void Main (string[] args) { file F =new file ("e:\\ music"); Showdir (f); } public static void Showdir (File dir) { System.out.println (dir); File[] Files =dir.listfiles (); for (file file:files) { if (file.isdirectory ())) showdir (file); else System.out.println (file);}}}
Moving files
public class Test5 {public static void Main (string[] args) { file F1 = new File ("d:\\"); MoveFile (F1); } public static void MoveFile (File dir) { file[] files=dir.listfiles (); for (file file:files) { if (file.isdirectory ())) moveFile (file); else{ if (File.getname (). EndsWith (". Java")) File.renameto (New file ("c:\\jad\\" + file.getname (). SUBSTRING (0,file.getname (). LastIndexOf ('. ')) + ". Jad"));}}}
Java File class methods use detailed