write
  public static void write(CharSequence from, File to, Charset charset) throws IOException {
asCharSink(to, charset, new FileWriteMode[0]).write(from);
}
write
  public static void write(byte[] from, File to) throws IOException {
asByteSink(to, new FileWriteMode[0]).write(from);
}

Files类提供了几种方法来通过ByteSink和CharSink类操作文件。

readLines
 public static List<String> readLines(File file, Charset charset) throws IOException {
return (List)readLines(file, charset, new LineProcessor() {
final List<String> result = Lists.newArrayList(); public boolean processLine(String line) {
this.result.add(line);
return true;
} public List<String> getResult() {
return this.result;
}
});
} public static <T> T readLines(File file, Charset charset, LineProcessor<T> callback) throws IOException {
return asCharSource(file, charset).readLines(callback);
}

这个readLines的重载,需要我们实现一个LineProcessor的泛型接口,在这个接口的实现方法processLine方法中我们可以对行文本进行处理,getResult方法可以获得一个最终的处理结果,一般是大文件的读取会用到这个。

另外还有readBytes方法可以对文件的字节做处理,readFirstLine可以返回第一行的文本,Files.toString(File,Charset)可以返回文件的所有文本内容。

equal
  public static boolean equal(File file1, File file2) throws IOException {
Preconditions.checkNotNull(file1);
Preconditions.checkNotNull(file2);
if(file1 != file2 && !file1.equals(file2)) {
long len1 = file1.length();
long len2 = file2.length();
return len1 != 0L && len2 != 0L && len1 != len2?false:asByteSource(file1).contentEquals(asByteSource(file2));
} else {
return true;
}
}

Guava中提供了{*}Files.equal(File,File)*方法来比较两个文件的内容是否完全一致

Files的更多相关文章

  1. Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ...

    Conversion to Dalvik format failed: Unable to execute dex: Multiple dex files define ... 这个错误是因为有两个相 ...

  2. The type javax.ws.rs.core.MediaType cannot be resolved. It is indirectly referenced from required .class files

    看到了http://stackoverflow.com/questions/5547162/eclipse-error-indirectly-referenced-from-required-clas ...

  3. 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\106f9ae8\cc0e1

    在本地开发环境没问题,但是发布到服务器出现:未能写入输出文件"c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.Ne ...

  4. Find and delete duplicate files

    作用:查找指定目录(一个或多个)及子目录下的所有重复文件,分组列出,并可手动选择或自动随机删除多余重复文件,每组重复文件仅保留一份.(支持文件名有空格,例如:"file  name" ...

  5. Android Duplicate files copied in APK

    今天调试 Android 应用遇到这么个问题: Duplicate files copied in APK META-INF/DEPENDENCIES File 1: httpmime-4.3.2.j ...

  6. Oracle客户端工具出现“Cannot access NLS data files or invalid environment specified”错误的解决办法

    Oracle客户端工具出现"Cannot access NLS data files or invalid environment specified"错误的解决办法 方法一:参考 ...

  7. files list file for package 'xxx' is missing final newline

    #!/usr/bin/python # 8th November, 2009 # update manager failed, giving me the error: # 'files list f ...

  8. Mac osx 安装PIL出现Some externally hosted files were ignored (use --allow-external PIL to allow).

    出现这个问题Some externally hosted files were ignored (use --allow-external PIL to allow)的主要原因是PIL的一些依赖库还没 ...

  9. 【Linux】Too many open files

    ZA 的BOSS 最近出现Too many open files 异常,这个异常一般是由于打开文件数过多引起, 最常见原因是某些连接一致未关闭 记录一些排查用到的指令 查看每个用户最大允许打开文件数量 ...

  10. [转]html5表单上传控件Files API

    表单上传控件:<input type="file" />(IE9及以下不支持下面这些功能,其它浏览器最新版本均已支持.) 1.允许上传文件数量 允许选择多个文件:< ...

随机推荐

  1. TestDirector创建域或工程

    一.打开TestDirector 1.打开TestDirector,进入如下页面 点击左上角"Site Administrator"进入 2.在输入框里输入正确的Site Admi ...

  2. QT使用WOL实现远程一键开机(局域网,需要目标电脑的主板支持,并且插上网线,用udpSocket.writeDatagram一句话就可以)

    功能:让关机的电脑一键开机,需要目标电脑的主板支持,并且插上网线: 效果:相当于手动按了一下目标电脑的开关机按钮. 没啥技术含量,简单开说... 1.获取目标机MAC地址 QByteArray sMa ...

  3. Bridging signals---hdu1950(最长上升子序列复杂度n*log(n) )

     题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1950 一直只知道有除n*n的算法之外的求LIS,但是没学过,也没见过,今天终于学了一下,dp[i]表 ...

  4. 使用 Dalvik 调试监控服务 (DDMS) 工具

    Android 附带一个叫Dalvik 调试监控服务 (DDMS) 的调试工具,它提供端口转发服务.在设备上的屏幕捕获,设备上的线程和堆栈信息, logcat,进程, 和无线状态信息,接收呼叫和SMS ...

  5. sql 随笔 2015-08-07

    xls 导入数据库 --删除现有数据 DELETE FROM dbo.PhoneList --插入数据 insert into dbo.PhoneList --读取xls数据 ) , as [Enab ...

  6. [IT] 关闭笔记本的蜂鸣提示

    很久没有写什么了,今天开过workshop, 稍微放松些, 一时动念上来看看,没想到最近一篇都是2010年的了, 不得不感叹时光流逝之快啊. 那就写点什么吧. 现在每天工作用的DELL笔记本,性能差强 ...

  7. MySQL —— 程序连接时的驱动名称和URL

    CONNECTION_DRIVER :  com.mysql.jdbc.Driver  CONNECTION_URL :    jdbc:mysql://localhost/DB_NAME 

  8. svn版本库包含多个项目 ; git svn clone; 某一个子项目,有多个分支;

    情况描述: 公司的svn版本库,包含了多个项目,每个项目对应于1个文件夹 假设版本库名字为Main,其下的项目用 A项目,对应文件夹A B项目,对应文件夹B 通过git svn clone获取了svn ...

  9. [HIHO1300]展胜地的鲤鱼旗(栈,dp)

    题目链接:http://hihocoder.com/problemset/problem/1300 给一个字符串,只包含'('和')',问存在多少个子串似的括号是匹配的. 匹配规则在题干中描(蒻)述( ...

  10. MySQL之Join

    参见MySQL(以5.1为例)中官方手册:MySQL官方手册-JOIN 假设有以下几个表 t1 id book 1 java 2 c++ 3 php t2 id author 2 zhang 3 wa ...