File 文件类
常用的file方法
public static void fileDemo() throws IOException {
File file = new File("e:\\test.txt");
System.out.println(file.exists());
System.out.println(file.createNewFile());
System.out.println(file.canExecute());
System.out.println(file.canRead());
System.out.println(file.canWrite());
System.out.println(file.getAbsolutePath());
System.out.println(file.getAbsoluteFile());
System.out.println(file.getParent());
System.out.println(file.getParentFile());
System.out.println(file.getTotalSpace());
System.out.println(file.getUsableSpace());
System.out.println(file.delete());
file.mkdir();
file.mkdirs();
file.delete();
}
创建文件
public static void createFile() throws IOException {
File file = new File("E:/a/b/c/d/e/f/g/h/test.txt");
File parentFile = file.getParentFile();
System.out.println(parentFile);
if(!parentFile.exists()){
parentFile.mkdirs();
}
file.createNewFile();
}
删除文件目录
public static void del(File file) {
if(file.isDirectory()){
if(file.list().length>0){
for (File listFile : file.listFiles()) {
del(listFile);
}
}
System.out.println("删除文件夹:"+file.getName());
file.delete();
}
if(file.isFile()){
System.out.println("删除文件:"+file.getName());
file.delete();
}
}