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);
}
 
 
 
 
 
 
 
 

最新文章

  1. 【微信开发】—7200s限制,非数据库
  2. java多线程--几个多线程面试题小结
  3. PetaPoco入门(一)
  4. JVM内存结构之二--新生代及新生代里的两个Survivor区(下一轮S0与S1交换角色,如此循环往复)、常见调优参数
  5. 审核流(3)低调奢华,简单不凡,实例演示-SNF.WorkFlow--SNF快速开发平台3.1
  6. 一个html5开发工具
  7. HTML 图像
  8. C#实现监控网络流量
  9. RHCE7.0练习题汇总[转]
  10. jQuery遍历json
  11. Linux 的使用基础---Linux的常用命令
  12. Java线程(十):CAS
  13. Android Folding View(折叠视图、控件)
  14. Django资源
  15. java.util.concurrent包下集合类的特点与适用场景
  16. Hibernate学习(六)———— cascade(级联)和inverse关系详解
  17. 优化MySQL性能的几种方法-总结
  18. scipy.stats
  19. Spring 学习教程(二): IOC/DI+AOP
  20. JSON.parse()与JSON.stringify()高级用法

热门文章

  1. which/whereis
  2. jQuery系列(六):jQuery的文档操作
  3. 8.5 JavaScript的BOM(二)
  4. 在windows下安装lxml 报错error: Unable to find vcvarsall.bat
  5. 【零基础】speech driven animation中文安装使用指南
  6. PIMPL(private implementantion)模式(转载)
  7. 【Spark机器学习速成宝典】推荐引擎——协同过滤
  8. 黑马lavarel教程---9、缓存操作
  9. Qt DLL总结【二】-创建及调用QT的 DLL
  10. nginx 499 错误码