Guava源码阅读-io-Files
2024-10-06 20:39:56
package com.google.common.io;
今天阅读一个非常常用的类Files,文件操作类。
readLines(File file, Charset charset),这个方法将File按行存入list<String>中。
return readLines(
file,
charset, //编码方式,通常都是utf-8
new LineProcessor<List<String>>() {
final List<String> result = Lists.newArrayList(); @Override
public boolean processLine(String line) {
result.add(line);
return true;
} @Override
public List<String> getResult() {
return result;
}
});
public static <T> T readLines(File file, Charset charset, LineProcessor<T> callback) //这个方法在将文件按行存储为Lists<String>时,同时调用行处理方法,只有满足要求的结果才会存到结果中。
throws IOException {
return asCharSource(file, charset).readLines(callback);
}
public static String toString(File file, Charset charset) throws IOException { //将文件转化为String,并返回,包含文件中的所有字符,包括换行符
return asCharSource(file, charset).read();
}
public static boolean equal(File file1, File file2) throws IOException //如果这两个文件相同,则返回true。这时不仅是内容,还包括文件长度。
if (file1 == file2 || file1.equals(file2)) {
return true;
}
if (len1 != 0 && len2 != 0 && len1 != len2) {
return false;
}
public static void copy(File from, File to) throws IOException { //拷贝一个文件里的所以字符给另一个文件
checkArgument(!from.equals(to), "Source %s and destination %s must be different", from, to);
asByteSource(from).copyTo(asByteSink(to));
}
public static void write(CharSequence from, File to, Charset charset) throws IOException {
//将指定内容写入文件,如果文件原本存在内容,则覆盖
asCharSink(to, charset).write(from);
}
public static void append(CharSequence from, File to, Charset charset) throws IOException { //追加文件,将指定内容追加到文件尾
write(from, to, charset, true);
}
最新文章
- 【微信开发】—7200s限制,非数据库
- java多线程--几个多线程面试题小结
- PetaPoco入门(一)
- JVM内存结构之二--新生代及新生代里的两个Survivor区(下一轮S0与S1交换角色,如此循环往复)、常见调优参数
- 审核流(3)低调奢华,简单不凡,实例演示-SNF.WorkFlow--SNF快速开发平台3.1
- 一个html5开发工具
- HTML 图像
- C#实现监控网络流量
- RHCE7.0练习题汇总[转]
- jQuery遍历json
- Linux 的使用基础---Linux的常用命令
- Java线程(十):CAS
- Android Folding View(折叠视图、控件)
- Django资源
- java.util.concurrent包下集合类的特点与适用场景
- Hibernate学习(六)———— cascade(级联)和inverse关系详解
- 优化MySQL性能的几种方法-总结
- scipy.stats
- Spring 学习教程(二): IOC/DI+AOP
- JSON.parse()与JSON.stringify()高级用法