1.下载:
网址:apache.org---下拉找到 APACHE PROJECT LIST---Commons---IO---Download now!---{Linux---Binaries---commons-io-2.6-bin.tar.gz | Windos--Binaries---commons-io-2.6-bin.zip}
2.解压后将jar包复制到项目lib文件夹下
右键---Build Path---Add to Buile Path
常用操作:
/**
* 学习使用Commonsio
* @author NI
*
*/
public class TestCommonsIO {
public static void main(String[] args) throws IOException {
//文件大小、目录大小
long len = FileUtils.sizeOf(new File("src\\com\\kzw\\ioc\\TestCommonsIO.java"));
System.out.println(len);
//获得子文件
Collection<File> file = FileUtils.listFiles(new File("D:\\eclipse-workspace\\IOToCommons"),
EmptyFileFilter.NOT_EMPTY,null);
for(File files :file) {
System.out.println(files.getAbsolutePath());
}
//读取
List<String> str = FileUtils.readLines(new File("src\\com\\kzw\\ioc\\aaa.txt"),"UTF-8");
for(String ss :str) {
System.out.println(ss);
}
String str1 = FileUtils.readFileToString(new File("src\\com\\kzw\\ioc\\aaa.txt"),"UTF-8");
System.out.println(str1);
//写出
FileUtils.write(new File("src\\com\\kzw\\ioc\\aaa.txt"), "asdsdsdsdsdsxxxxx\r\n","UTF-8",true);
//写出列表
List<String> list = new ArrayList<String>();
list.add("asd1");
list.add("asd2");
list.add("asd3");
FileUtils.writeLines(new File("src\\com\\kzw\\ioc\\aaa.txt"),list, "...",true);
//复制
FileUtils.copyFile(new File("a.png"), new File("a-cpoy.png"));
//复制文件到目录
FileUtils.copyFileToDirectory(new File("a.png"), new File("lib"));
//拷贝url内容
String st2 = "https://www.baidu.com";
FileUtils.copyURLToFile(new URL(st2),new File("aaa.txt"));
String st = IOUtils.toString(new URL("http://www.baidu.com"),"UTF-8");
System.out.println(st);
}
}