第十篇 IO流技术(十)
package com.zzp.commons; import java.io.File; import org.apache.commons.io.FileUtils; /** * * 大小 * @author java * */ public class CIOTest01 { public static void main(String[] args) { //文件的大小 long len = FileUtils.sizeOf(new File("src/com/zzp/commons/CIOTest01.java")); System.out.println(len); //目录的大小 len = FileUtils.sizeOf(new File("F:/workspace1/IO_Day04/")); System.out.println(len); } }
package com.zzp.commons; import java.io.File; import java.util.Collection; import org.apache.commons.io.FileUtils; import org.apache.commons.io.filefilter.DirectoryFileFilter; import org.apache.commons.io.filefilter.EmptyFileFilter; import org.apache.commons.io.filefilter.FileFilterUtils; import org.apache.commons.io.filefilter.SuffixFileFilter; /** * * 列出子孙级 * @author java * */ public class CIOTest02 { public static void main(String[] args) { Collection<File> listFiles = FileUtils.listFiles(new File("F:/workspace1/IO_Day04"), EmptyFileFilter.NOT_EMPTY, null); for (File file : listFiles) { System.out.println(file.getAbsolutePath()); } System.out.println("==============================="); listFiles = FileUtils.listFiles(new File("F:/workspace1/IO_Day04"), EmptyFileFilter.NOT_EMPTY, DirectoryFileFilter.INSTANCE); for (File file : listFiles) { System.out.println(file.getAbsolutePath()); } System.out.println("==============================="); listFiles = FileUtils.listFiles(new File("F:/workspace1/IO_Day04"), new SuffixFileFilter("java"), DirectoryFileFilter.INSTANCE); for (File file : listFiles) { System.out.println(file.getAbsolutePath()); } System.out.println("==============================="); listFiles = FileUtils.listFiles(new File("F:/workspace1/IO_Day04"), FileFilterUtils.or(new SuffixFileFilter("java"), new SuffixFileFilter("class"),EmptyFileFilter.EMPTY), DirectoryFileFilter.INSTANCE); for (File file : listFiles) { System.out.println(file.getAbsolutePath()); } System.out.println("==============================="); listFiles = FileUtils.listFiles(new File("F:/workspace1/IO_Day04"), FileFilterUtils.and(new SuffixFileFilter("java"), EmptyFileFilter.NOT_EMPTY), DirectoryFileFilter.INSTANCE); for (File file : listFiles) { System.out.println(file.getAbsolutePath()); } System.out.println("==============================="); } }
package com.zzp.commons; import java.io.File; import java.io.IOException; import java.util.Collection; import java.util.List; import org.apache.commons.io.FileUtils; import org.apache.commons.io.LineIterator; import org.apache.commons.io.filefilter.DirectoryFileFilter; import org.apache.commons.io.filefilter.EmptyFileFilter; import org.apache.commons.io.filefilter.FileFilterUtils; import org.apache.commons.io.filefilter.SuffixFileFilter; /** * * 读取内容 * @author java * */ public class CIOTest03 { public static void main(String[] args) throws IOException { //读取文件 String readFileToString = FileUtils.readFileToString(new File("1.txt"), "gbk"); System.out.println(readFileToString); //读取成字节数组 byte[] readFileToByteArray = FileUtils.readFileToByteArray(new File("1.txt")); System.out.println(readFileToByteArray.length); //逐行读取 List<String> readLines = FileUtils.readLines(new File("1.txt"), "gbk"); for (String string : readLines) { System.out.println(string); } LineIterator lineIterator = FileUtils.lineIterator(new File("1.txt"), "gbk"); while(lineIterator.hasNext()){ System.out.println(lineIterator.next()); } } }
package com.zzp.commons; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Collection; import java.util.List; import org.apache.commons.io.FileUtils; import org.apache.commons.io.LineIterator; import org.apache.commons.io.filefilter.DirectoryFileFilter; import org.apache.commons.io.filefilter.EmptyFileFilter; import org.apache.commons.io.filefilter.FileFilterUtils; import org.apache.commons.io.filefilter.SuffixFileFilter; /** * * 写出内容 * @author java * */ public class CIOTest04 { public static void main(String[] args) throws IOException { FileUtils.write(new File("2.txt"), "生活不易,且行且珍惜\r\n","gbk"); FileUtils.writeStringToFile(new File("2.txt"), "是真的\r\n", "gbk",true); //写出列表 List<String> datas =new ArrayList<String>(); datas.add("张三"); datas.add("李四"); datas.add("王五"); FileUtils.writeLines(new File("2.txt"), datas, "......", true); } }
package com.zzp.commons; import java.io.File; import java.io.IOException; import java.net.URL; import java.util.ArrayList; import java.util.Collection; import java.util.List; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.io.LineIterator; import org.apache.commons.io.filefilter.DirectoryFileFilter; import org.apache.commons.io.filefilter.EmptyFileFilter; import org.apache.commons.io.filefilter.FileFilterUtils; import org.apache.commons.io.filefilter.SuffixFileFilter; /** * * 拷贝 * @author java * */ public class CIOTest05 { public static void main(String[] args) throws IOException { //复制文件 /*FileUtils.copyFile(new File("1.txt"), new File("3.txt"));*/ //复制文件到目录 /*FileUtils.copyFileToDirectory(new File("1.txt"), new File("lib"));*/ //复制目录到目录 //FileUtils.copyDirectoryToDirectory(new File("lib"), new File("lib2")); //复制目录 //FileUtils.copyDirectory(new File("lib"), new File("lib2")); //拷贝url内容 /*String url = "https://pic2.zhimg.com/v2-7d01cab20858648cbf62333a7988e6d0_qhd.jpg"; FileUtils.copyURLToFile(new URL(url), new File("marvel.jpg"));*/ String string = IOUtils.toString(new URL("http://www.163.com"),"GBK"); System.out.println(string); } }