commonsIO工具类的使用
1、下载jar文件:commonsIO工具包
2、导入到工程中去
操作
文件和文件夹的大小:FileUtils.sizeOf()
public class io_commonsIO_test {
public static void main(String[] args) {
//文件大小
long len= FileUtils.sizeOf(new File("E:/idea/io_commonsIO_study/io_commonsIO_study.iml"));
System.out.println(len);
//文件夹大小
len=FileUtils.sizeOfDirectory(new File("E:/idea/io_commonsIO_study"));
System.out.println(len);
}
}
遍历子孙级文件
public class io_commonsIO_test02 {
public static void main(String[] args) {
//过滤空文件,null为第一层目录
Collection<File> files=FileUtils.listFiles(new File("E:/idea/io_commonsIO_study"), EmptyFileFilter.NOT_EMPTY,null);
for (File fs:files
) {
System.out.println(fs.getAbsolutePath());
}
System.out.println("--------------------------");
//过滤空文件,递归目录
files=FileUtils.listFiles(new File("E:/idea/io_commonsIO_study"), EmptyFileFilter.NOT_EMPTY, DirectoryFileFilter.INSTANCE);
for (File fs:files
) {
System.out.println(fs.getAbsolutePath());
}
System.out.println("--------------------------");
//递归列出后缀为.java的文件
files=FileUtils.listFiles(new File("E:/idea/io_commonsIO_study"),
new SuffixFileFilter("java"), DirectoryFileFilter.INSTANCE);
for (File fs:files
) {
System.out.println(fs.getAbsolutePath());
}
System.out.println("--------------------------");
//递归列出后缀为.java和.class的文件和空文件
files=FileUtils.listFiles(new File("E:/idea/io_commonsIO_study"),
FileFilterUtils.or(new SuffixFileFilter("java"),new SuffixFileFilter("class"),EmptyFileFilter.EMPTY),DirectoryFileFilter.INSTANCE);
for (File fs:files
) {
System.out.println(fs.getAbsolutePath());
}
System.out.println("--------------------------");
}
}
读取文件
public class io_commonsIO_test03 {
public static void main(String[] args) throws IOException {
//读取文件
String msg =FileUtils.readFileToString(new File("empty.txt"),"utf-8");
System.out.println(msg);
System.out.println("----------------");
//读取文件存入字节数组
byte[]datas=FileUtils.readFileToByteArray(new File("empty.txt"));
System.out.println(datas.length);
System.out.println("------------------");
//逐行读取
List<String>list=FileUtils.readLines(new File("empty.txt"),"utf-8");
for (String sr:list
) {
System.out.println(sr);
}
System.out.println("---------------");
}
}
写出内容
public class io_commonsIO_test04 {
public static void main(String[] args) throws IOException {
//写出文件
FileUtils.write(new File("happycode.txt"),"山东扒鸡,买一送一!\r\n","utf-8");
FileUtils.writeStringToFile(new File("happycode.txt"),"山东扒鸡,买一送一!\r\n","utf-8",true);
//写出列表
List<String>datas=new ArrayList<>();
datas.add("扒鸡");
datas.add("山扒");
datas.add("德扒");
FileUtils.writeLines(new File("happycode.txt"),datas,"===",true);
}
}
文件拷贝
public class io_commonsIO_test05 {
public static void main(String[] args) throws IOException {
复制文件
FileUtils.copyFile(new File("io.png"),new File("io_copy.png"));
复制文件到目录
FileUtils.copyFileToDirectory(new File("io.png"),new File("lib"));
复制目录到目录,成为子目录
FileUtils.copyDirectoryToDirectory(new File("lib"),new File("lib2"));
复制目录
FileUtils.copyDirectory(new File("lib"),new File("lib2"));
拷贝URl内容
String url="http://down-update.qq.com/dnf/website/wallpaper/201907/201907b.rar";
FileUtils.copyURLToFile(new URL(url),new File("C:/Users/tfp12/Desktop/DNF.rar"));
String baidu=IOUtils.toString(new URL("http://www.baidu.com"),"utf-8");
System.out.println(baidu);
}
}