package cn.bruce.file; import java.io.File; import java.io.IOException; public class FileDemo2 { public static void main(String[] args) throws IOException { fun1(); fun2(); fun3(); fun4(); fun5(); fun6(); fun6_1(); } public static void fun1() throws IOException { File file = new File("E:\A.TXT"); Boolean B = file.createNewFile();// 当不存在此名称文件时创建文件 System.out.println(B); Boolean B1 = file.delete();// 删除此名称文件 System.out.println(B1); } public static void fun2() throws IOException { File file = new File("E:\A\b\c"); Boolean B = file.mkdirs();// 当不存在此名称文件时创建多层次文件夹 System.out.println(B); File file1 = new File("E:\b"); Boolean B1 = file1.delete();// 删除此名称单层文件夹 System.out.println(B1); } public static void fun3() throws IOException { File file1 = new File("E:\b\A.txt"); Long length = file1.length();// 取得文件大小字节数 System.out.println(length); } public static void fun4() throws IOException { File file1 = new File("E:\b\A.txt"); Boolean B1 = file1.exists();// 判断路径存不存在 System.out.println(B1); } public static void fun5() throws IOException { File file1 = new File("E:\b"); if (file1.exists())// 路径存在才去判断 { Boolean B1 = file1.isDirectory();// 判断是不是目录 System.out.println(B1); } } // 遍历目录1 public static void fun6() throws IOException { File file1 = new File("E:\b"); if (file1.exists())// 路径存在才去判断 { String[] s = file1.list();// 判断是不是目录 for (String string : s) { System.out.println(string); } } } // 遍历目录2-全名 推荐 public static void fun6_1() throws IOException { File file1 = new File("E:\b"); if (file1.exists())// 路径存在才去判断 { File[] s = file1.listFiles();// 判断是不是目录 for (File File : s) { System.out.println(File); } } } }