使用步骤:
1.Java Project下新建Folder文件夹,命名lib
2.lib文件夹下把Commons-io-2.4jar包复制进去
3.右键Commons-io-2.4jar包,点击Build Path,点击Add to Build Path
使用方法:
代码:
import org.apache.commons.io.FilenameUtils;public class Demo1Commons { public static void main(String[] args) { fun3(); } //FilenameUtils方法,获得拓展名,需要加上后缀名,才能获得后缀名 public static void fun1(){ String s=FilenameUtils.getExtension("c:\\5.txt"); System.out.println(s); } //获得文件名 public static void fun2(){ String name1=FilenameUtils.getName("c:\\5.txt"); System.out.println(name1); } //判断文件名的后缀 public static void fun3(){ boolean b= FilenameUtils.isExtension("5.txt","java"); System.out.println(b); }}
代码:
import java.io.File;import java.io.IOException;import org.apache.commons.io.FileUtils;public class Demo2Commons { public static void main(String[] args) throws IOException { fun4(); } //读取文本,返回字符串 public static void fun1() throws IOException{ String s=FileUtils.readFileToString(new File("c:\\4.txt")); System.out.println(s); } //将字符串直接写到文件夹中 public static void fun2() throws IOException{ FileUtils.writeStringToFile(new File("c:\\4.txt"), "春天到了"); } //复制文件 public static void fun3() throws IOException{ FileUtils.copyFile(new File("c:\\4.txt"), new File("d:\\4.txt")); } //复制文件夹 public static void fun4() throws IOException{ FileUtils.copyDirectory(new File("c:\\Intel"), new File("d:\\Intel")); } }