文件IO操作类库:org.apache.commons.io.FileUtils

文件IO操作推荐使用org.apache.commons.io.FileUtils 类库. 好处是无需显式开关IO流;

官方参考文档:

http://commons.apache.org/proper/commons-io/javadocs/api-release/org/apache/commons/io/FileUtils.html


一、使用方式:

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>

二、常用方法:

方法1:FileUtils.readLines

//FileUtils.readLines
File file = new File(input_filePath);
List<String> strings = FileUtils.readLines(file, "utf-8");
strings.forEach(System.out::println);

方法2:FileUtils.readFileToString

// FileUtils.readFileToString
File file = new File(input_filePath);
String s = FileUtils.readFileToString(file, "utf-8");
System.out.println(s);

方法3:/FileUtils.write

//FileUtils.write
File file = new File(input_filePath);
String s = FileUtils.readFileToString(file, "utf-8");
FileUtils.write(new File(out_file_path),s,"utf-8");

方法4:FileUtils.writeStringToFile

//FileUtils.writeStringToFile
File file = new File(input_filePath);
String s = FileUtils.readFileToString(file, "utf-8");
FileUtils.writeStringToFile(new File(out_file_path2),s,"utf-8");

方法5:FileUtils.contentEquals

//FileUtils.contentEquals(file1, file2)
File file1 = new File(out_file_path);
File file2 = new File(out_file_path2);
boolean isSame = FileUtils.contentEquals(file1, file2);
System.out.println(isSame);

方法6:FileUtils.copyFile

//FileUtils.copyFile
FileUtils.copyFile(new File(out_file_path),new File(out_file_path_copy));

方法7:FileUtils.copyFileToDirectory

//FileUtils.copyFileToDirectory
FileUtils.copyFileToDirectory(new File(out_file_path),new File(dir_copy));

方法8:FileUtils.copyDirectory

//FileUtils.copyDirectory
FileUtils.copyDirectory(new File(dir),new File(dir_copy));

方法9:FileUtils.cleanDirectory

//FileUtils.cleanDirectory
FileUtils.cleanDirectory(new File(dir_copy));

方法10:FileUtils.deleteDirectory

//FileUtils.deleteDirectory
FileUtils.deleteDirectory(new File(dir_copy));

方法11:FileUtils.isFileOlder

//FileUtils.isFileOlder
boolean older = FileUtils.isFileOlder(new File(out_file_path), new Date());

方法12:FileUtils.isFileNewer

//FileUtils.isFileNewer
boolean newer = FileUtils.isFileNewer(new File(out_file_path), new Date());
System.out.println(newer);

方法13:FileUtils.deleteQuietly

//FileUtils.deleteQuietly
boolean deleteQuietly = FileUtils.deleteQuietly(new File(out_file_path_copy));
System.out.println(deleteQuietly);

方法14:FileUtils.moveDirectory

//FileUtils.moveDirectory
FileUtils.moveDirectory(new File(dir),new File(dir_move));

方法15:FileUtils.moveFile

//FileUtils.moveFile
FileUtils.moveFile(new File(input_filePath),new File(out_file_path_move));

方法16:FileUtils.directoryContains

//FileUtils.directoryContains
boolean b1 = FileUtils.directoryContains(new File(dir), new File(out_file_path));
System.out.println(b1);
FileUtils.copyFileToDirectory(new File(out_file_path),new File(dir));
boolean b2 = FileUtils.directoryContains(new File(dir), new File(out_file_path_contains));
System.out.println(b2);

方法17:FileUtils.getUserDirectory

//FileUtils.getUserDirectory
File directory = FileUtils.getUserDirectory();
System.out.println(directory);

方法18:FileUtils.lineIterator

//FileUtils.lineIterator
LineIterator iterator = FileUtils.lineIterator(new File(input_filePath));
while (iterator.hasNext()){
    System.out.println(iterator.nextLine());
};

 

上一篇:Natas27 Writeup(mysql溢出截断漏洞)


下一篇:SpringCloud Gateway使用实例