1.导包
导commons-io-2.4.jar包
2.
public class Demo1Commons { public static void main(String[] args) { fun3(); } // 判断文件名的后缀 public static void fun3(){ boolean s = FilenameUtils.isExtension("ll.txt", "java"); System.out.println(s); } // 获得文件名 public static void fun2(){ String name1 = FilenameUtils.getName("d:\ll.txt"); System.out.println(name1); } // FilenameUtils方法 获得拓展名 需要加上后缀名 才能获得后缀名 public static void fun(){ String n = FilenameUtils.getExtension("d:\ll.txt"); System.out.println(n); } }
public class Demo2Commos { public static void main(String[] args) throws IOException { fun3(); } // 复制文件夹 public static void fun3() throws IOException{ FileUtils.copyDirectoryToDirectory(new File("c:\AppData"), new File("d:\AppData")); } // 复制文件 public static void fun2() throws IOException{ FileUtils.copyFile(new File("c:\aaa.txt"), new File("d:\aaa.txt")); } //将字符串直接写到文件中 public static void fun1() throws IOException{ FileUtils.writeStringToFile(new File("c:\aaa.txt"), "春天来了"); } //读取文本 返回字符串 public static void fun() throws IOException{ String s = FileUtils.readFileToString(new File("c:\ll.txt")); System.out.println(s); } }