常用API
package coreJava;
import java.io.File;
import java.io.IOException;
public class FileDemo {
public static void main(String[] args) {
//了解构造函数的情况,查帮助
File file = new File("G:\\workspace_JAVA\\coreJava\\src\\coreJava\\test");
//File file2 = new File("G:"+File.separator);//分隔符
//System.out.println(file.exists());
if(!file.exists())
file.mkdir();//file.mkdirs();//多级目录
else
file.delete();
//是否是一个目录
System.out.println(file.isDirectory());
//是否是一个文件
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();
//常用的File对象的API
System.out.println(file);//file.toString()的内容
System.out.println(file.getAbsolutePath());//抽象的路径
System.out.println(file.getName());//文件的名字
System.out.println(file2.getName());
System.out.println(file.getParent());
System.out.println(file2.getParent());
System.out.println(file.getParentFile().getAbsolutePath());
}
}
运行结果
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
遍历目录
package coreJava;
import java.io.File;
import java.io.IOException;
public class FileUtils {
/**
* 列出指定目录下(包括其子目录)的所有文件
* @param args
*/
public static void listDirectory(File dir) throws IOException{
// TODO Auto-generated method stub
if(!dir.exists()){
throw new IllegalArgumentException("目录:"+dir+"不存在.");
}
if(!dir.isDirectory()){
throw new IllegalArgumentException(dir+"不是目录");
}
/*String[] filenames = dir.list();//返回字符串数组,直接子的名称,不包含子目录下的内容
for(String string:filenames){
System.out.println(dir+"\\"+string);
}*/
//如果要遍历子目录下的内容就需要构造成File对象做递归操作,File提供了直接返回子目录的抽象
File[] files = dir.listFiles();//直接返回的是直接子目录(文件的抽象)
if(files!=null&&files.length>0){
for (File file : files) {
if(file.isDirectory()){
//递归
listDirectory(file);
}
else{
System.out.println(file);
}
}
}
}
}
测试方法
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 stub
FileUtils.listDirectory(new File("G:\\workspace_JAVA"));
}
}
运行结果
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