Common APIs
Package Corejava;
Import Java.io.File;
Import java.io.IOException; public class Filedemo {public static void main (string[] args) {//Understand the constructor, check Help file file = new file
("G:\\workspace_java\\corejava\\src\\corejava\\test");
File File2 = new file ("G:" +file.separator);//Separator//system.out.println (file.exists ());
if (!file.exists ()) File.mkdir ();//file.mkdirs ();//Multilevel Directory else File.delete ();
is a directory System.out.println (File.isdirectory ());
Whether it is a file System.out.println (File.isfile ());
File File2 = new file ("G:\\workspace_java\\corejava\\src\\corejava\\test.txt");
File File2 = new file ("G:\\workspace_java\\corejava\\src\\corejava", "test.txt");
if (!file2.exists ()) try {file2.createnewfile ();
catch (IOException e) {//TODO auto-generated catch block E.printstacktrace ();
} else File2.delete (); The common file object's API System.out.println (file),//file.tostring () content System.out.println (File.getabsolutepath ());
/abstract Path System.out.println (File.getname ());//The name of the file System.out.println (File2.getname ());
System.out.println (File.getparent ());
System.out.println (File2.getparent ());
System.out.println (File.getparentfile (). GetAbsolutePath ());
}
}
Run results
True
false
G:\workspace_JAVA\coreJava\src\coreJava\test
G:\workspace_JAVA\coreJava\src\coreJava\ Test
Test
test.txt
G:\workspace_JAVA\coreJava\src\coreJava
G:\workspace_JAVA\coreJava\src\ Corejava
G:\workspace_JAVA\coreJava\src\coreJava
Traverse Directory
Package Corejava;
Import Java.io.File;
Import java.io.IOException; public class FileUtils {/** * lists all files in the specified directory (including its subdirectories) * @param args/public static void Listdirect Ory (File dir) throws ioexception{//TODO auto-generated Method stub if (!dir.exists ()) {throw
New IllegalArgumentException ("Directory: +dir+" does not exist. ");}
if (!dir.isdirectory ()) {throw new IllegalArgumentException (dir+ "not Directory");
}/*string[] filenames = dir.list ()//Returns a string array, the name of the direct child, and does not contain the contents of the subdirectory for (String string:filenames) {
System.out.println (dir+ "\" +string);
}*///If you want to traverse the contents of the subdirectory, you need to construct a file object to do the recursive operation, file provides an abstract file[that directly returns the subdirectory] files = Dir.listfiles (); Direct subdirectories (Abstract of the file) are returned directly. if (files!=null&&files.length>0) {for (File file:files) {if (file.isdirect
Ory ()) {//recursive listdirectory (file);
} else{ System.out.println (file);
}
}
}
}
}
Test method
Package Corejava;
Import Java.io.File;
Import java.io.IOException;
public class Test1 {public
static void Main (string[] args) throws IOException {
//TODO auto-generated method Stu b
Fileutils.listdirectory (New File ("G:\\workspace_java"));
}
Run results
g:\workspace_java\.metadata\.plugins\org.eclipse.pde.core\eclipse Application\ Org.eclipse.osgi\899\0\.cp\icons\wtp_icon_x32.gif G:\workspace_JAVA\.metadata\.plugins\org.eclipse.pde.core\ Eclipse Application\org.eclipse.osgi\899\0\.cp\icons\wtp_icon_x48.gif G:\workspace_JAVA\.metadata\.plugins\ Org.eclipse.pde.core\eclipse application\org.eclipse.osgi\899\0\.cp\images\01.png G:\workspace_JAVA\.metadata\. Plugins\org.eclipse.pde.core\eclipse application\org.eclipse.osgi\899\0\.cp\images\02a.png G:\workspace_JAVA\. Metadata\.plugins\org.eclipse.pde.core\eclipse Application\org.eclipse.osgi\899\0\.cp\images\02b.png G:\ Workspace_java\.metadata\.plugins\org.eclipse.pde.core\eclipse application\org.eclipse.osgi\899\0\.cp\images\ 02b_o.png G:\workspace_JAVA\.metadata\.plugins\org.eclipse.pde.core\Eclipse application\org.eclipse.osgi\899\0\. Cp\images\02c.png