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. PHP:错误 Deprecated: Function split() is deprecated in ... 解决办法

    PHP:错误 Deprecated: Function split() is deprecated in ... 解决办法 PHP5.3 split() 不建议使用的原因:PHP 5.3.0 之后的r ...

  2. React事件函数简介

    一.事件汇总 二.例子 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta charset= ...

  3. MyBatis学习总结_04_解决字段名与实体类属性名不相同的冲突

    一.准备演示需要使用的表和数据 CREATE TABLE orders( order_id INT PRIMARY KEY AUTO_INCREMENT, order_no VARCHAR(20), ...

  4. Spring 事务配置的几种方式

    参考:http://www.blogjava.net/robbie/archive/2009/04/05/264003.html http://www.cnblogs.com/appleat/arch ...

  5. Hadoop、Pig、Hive、Storm、NOSQL 学习资源收集

    (一)hadoop 相关安装部署 1.hadoop在windows cygwin下的部署: http://lib.open-open.com/view/1333428291655 http://blo ...

  6. emacs 快捷键笔记

    C-d C-aM-aC-eM-e===复制黏贴M-d    kill-wordM-del     backward-kill-wordM-k     kill-sentenceC-x del back ...

  7. MYSQL语句中SELECT语句及其子句的执行顺序

    SELECT语句的执行的逻辑查询处理步骤:(8)SELECT (9)DISTINCT(11)<TOP_specification> <select_list>(1)FROM & ...

  8. Eclipse下运行Maven项目提示缺少maven-resources-plugin:2.4.3

    将一个手动创建的Maven项目(命令行下可正常运行)导入到Eclipse中,运行时提示这样的错误信息:[ERROR] Plugin org.apache.maven.plugins:maven-res ...

  9. 第7篇 ORACLE EBS DEMO虚拟机环境的安装

    ERP信息系统的实施不仅要求懂得道理方面的知识,更要侧重于应用实践.为了有一个稳定的测试环境.初学者可以自己搭建一个EBS DEMO环境.本节介绍EBS DEMO环境虚拟机的安装.一. 安装前的准备( ...

  10. linux常用头文件及说明

    linux常用头文件及说明 1. Linux中一些头文件的作用: <assert.h>:ANSI C.提供断言,assert(表达式)<glib.h>:GCC.GTK,GNOM ...