writelines()方法将什么写入文件FileUtils使⽤介绍
FileUtils使⽤介绍
这篇⽂章我们会介绍FileUtils类相关的功能,它也是Apache Commons IO的⼀部分。它所提供的⽅法将我们常⽤的任务进⾏了包装,使我们写的代码更简洁易读。
1. FileUtils⽅法
我将介绍FileUtils类中⼀些重要的⽅法,并详细说明它们如何使⽤。FileUtils是⼀个静态类,这样意味着我们可以不⽤实例化就可以直接通过类去调⽤⽅法。
contentEquals(File file1, File file2):⽐较两个⽂件的内容,返回⽐较的结果。
copyDirectory(File source, File destination):复制整个⽂件夹到另外⼀个地⽅。
copyDirectory(File source, File destination, FileFilter filter):作⽤跟上⾯的⽅法类似,不过我们可以过滤⼀些指定的⽂件(⽐如名字,修改时时间等等)
copyFile(File srcFile, File destFile):拷贝⼀个⽂件到另外⼀个⽂件
deleteDirectory(File directory):递归的删除⼀个⽂件夹
getTempDirectory():返回表⽰系统临时⽬录的File对象
getUserDirectory():与上⾯相同,但这次代表⽤户⽬录。
lineIterator(File file):它创建了⼀个迭代器,可以按⾏遍历给定的⽂件。
sizeOfDirectory(File directory):它返回⽬录内容的⼤⼩
write(File file, CharSequence data):使⽤指定的编码将CharSequence写⼊⽂件中。
writeLines(File file, Collection<?> lines):将集合按⾏写⼊到⽂件中
2. FileUtils例⼦
import org.apachemons.io.FileUtils;
import org.apachemons.io.LineIterator;
import org.apachemons.io.filefilter.SuffixFileFilter;
stng.annotations.Test;
import java.io.File;
import java.io.IOException;
/**
* @ClassName FileUtilsTest
* @Description:
* @Author: liuyao
* @Date: 2019/4/10 9:50
* @Version: 1.0
**/
public class FileUtilsTest {
private static final String MAIN_PATH ="D:\\Workspace\\IdeaProjects"+
"\\pms_study\\src\\main\\java\\commonsio\\";
@Test
public void test()throws IOException {
File file1 = File(MAIN_PATH +"");
File file2 = File(MAIN_PATH +"");
System.out.println("Are cmpFile1 and cmpFile2 equal: "+ tEquals(file1, file2));
// 拷贝⽂件夹
System.out.println("Does the copiedPath exist: "+ File(MAIN_PATH +"copiedPath\\").exists());
// 只拷贝后缀为txt的⽂件
new SuffixFileFilter(".txt"));
// 输出该⽂件夹下的所有⽂件名
for(File f : File(MAIN_PATH +"copiedFilterPath\\").listFiles()){
System.out.println("Contents of copiedFilterPath: "+ f.getName());
}
// 拷贝⽂件
File File(MAIN_PATH+"");
System.out.println("Are cmpFile1 and copyOfFile1 equal: "+
// 删除⽂件夹
FileUtils.File(MAIN_PATH +"copiedFilterPath\\"));
for(File f: File(MAIN_PATH).listFiles()){
System.out.println("Contents of MAIN_PATH after deletion: "+ f.getName());
}
// 获得系统临时⽬录
System.out.println("Temp Dir: "+TempDirectory().getAbsolutePath());
// 获得⽤户⽬录
System.out.println("User Dir: "+UserDirectory().getAbsolutePath());
/
/ 按⾏遍历⽂件
LineIterator iterator=FileUtils.lineIterator(file2);
while(iterator.hasNext()){
System.out.println("cmpFile2 lines:"+());
}
// 统计⽂件夹⼤⼩
System.out.println("Size of Dir:"+FileUtils.File(MAIN_PATH))+" bytes"); // 将字符序列写⼊⽂件
File fileToWrite1 = File(MAIN_PATH +"");
FileUtils.write(fileToWrite1,"ccccccccc");
iterator=FileUtils.lineIterator(fileToWrite1);
while(iterator.hasNext()){
System.out.println("fileToWrite1 lines: "+ ());
}
}
}
输出结果为:
Are cmpFile1 and cmpFile2 equal: false
Does the copiedPath exist: true
Contents of copiedFilterPath:
Contents of copiedFilterPath:
Contents of copiedFilterPath: Contents of copiedFilterPath: Contents of copiedFilterPath:
Are cmpFile1 and copyOfFile1 equal: true
Contents of MAIN_PATH after deletion: Contents of MAIN_PATH after deletion: Contents of MAIN_PATH after deletion: copiedPath Contents of MAIN_PATH after deletion: Contents of MAIN_PATH after deletion: Contents of MAIN_PATH after deletion: FileUtilsTest.java Contents of MAIN_PATH after deletion: Git.png Contents of MAIN_PATH after deletion: IOUtilsTest.java Contents of MAIN_PATH after deletion:
Temp Dir: C:\Users\liuyao\AppData\Local\Temp
User Dir: C:\Users\liuyao
cmpFile2 lines:Hello World
cmpFile2 lines:asdf
Size of Dir:63814 bytes
fileToWrite1 lines: ccccccccc
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论