Files
public static void write(CharSequence from, File to, Charset charset) throws IOException {
asCharSink(to, charset, new FileWriteMode[0]).write(from);
}
public static void write(byte[] from, File to) throws IOException {
asByteSink(to, new FileWriteMode[0]).write(from);
}
Files类提供了几种方法来通过ByteSink和CharSink类操作文件。
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)可以返回文件的所有文本内容。
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的更多相关文章
- 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 ... 这个错误是因为有两个相 ...
- 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 ...
- 未能写入输出文件“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 ...
- Find and delete duplicate files
作用:查找指定目录(一个或多个)及子目录下的所有重复文件,分组列出,并可手动选择或自动随机删除多余重复文件,每组重复文件仅保留一份.(支持文件名有空格,例如:"file name" ...
- Android Duplicate files copied in APK
今天调试 Android 应用遇到这么个问题: Duplicate files copied in APK META-INF/DEPENDENCIES File 1: httpmime-4.3.2.j ...
- Oracle客户端工具出现“Cannot access NLS data files or invalid environment specified”错误的解决办法
Oracle客户端工具出现"Cannot access NLS data files or invalid environment specified"错误的解决办法 方法一:参考 ...
- 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 ...
- 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的一些依赖库还没 ...
- 【Linux】Too many open files
ZA 的BOSS 最近出现Too many open files 异常,这个异常一般是由于打开文件数过多引起, 最常见原因是某些连接一致未关闭 记录一些排查用到的指令 查看每个用户最大允许打开文件数量 ...
- [转]html5表单上传控件Files API
表单上传控件:<input type="file" />(IE9及以下不支持下面这些功能,其它浏览器最新版本均已支持.) 1.允许上传文件数量 允许选择多个文件:< ...
随机推荐
- awk除去重复行
awk去除重复行,思路是以每一行的$0为key,创建一个hash数组,后续碰到的行,如果数组里已经有了,就不再print了,否则将其print 测试文件: 用awk: 用sort+uniq好像出错了: ...
- Struts2笔记——ONGL表达式语言
OGNL是ObjectGraphic Navigation Language(对象图导航语言)的缩写,它是一个开源项目. Struts 2框架使用OGNL作为默认的表达式语言. ----------- ...
- 【ArcEngine入门与提高】Element(元素)、Annotation(注记)旋转
因项目需要,需要做一个旋转注记的工具.因为注记这玩意用的比较少,网上资源也很少,所以做起来相当头疼.在经过一番研究之后,终于搞清楚注记的存储原理了,原来是和Element的类似,只不过注记是要把Ele ...
- 【web性能】web性能测试工具推荐
WEB性能测试工具主要分为三种,一种是测试页面资源加载速度的,一种是测试页面加载完毕后页面呈现.JS操作速度的,还有一种是总体上对页面进行评价分析,下面分别对这些工具进行介绍,如果谁有更好的工具也请一 ...
- Android 自定义View,仿微信视频播放按钮
闲着,尝试实现了新版微信视频播放按钮,使用的是自定义View,先来个简单的效果图...真的很简单哈. 由于暂时用不到,加上时间原因,加上实在是没意思,加上……,本控件就没有实现自定义属性,有兴趣的朋友 ...
- Action的执行
异步Action的定义 两种异步Action方法的定义 xxxAsync/xxxCompleted 这种形式的异步只能定义在实现了AsyncController的Controller中.针对Task的 ...
- USACO Section 3.3: Riding the Fences
典型的找欧拉路径的题.先贴下USACO上找欧拉路径的法子: Pick a starting node and recurse on that node. At each step: If the no ...
- 压缩工具类 - ZipUtils.java
压缩工具类,提供压缩文件.解压文件的方法. 源码如下:(点击下载 - ZipUtils.java .FolderUtils.java.ant-1.7.0.jar.commons-io-2.4.jar. ...
- Android内存管理(4)*官方教程 含「高效内存的16条策略」 Managing Your App's Memory
Managing Your App's Memory In this document How Android Manages Memory Sharing Memory Allocating and ...
- Linux日常使用指令大全
Linux日常使用指令大全 Java代码 www.ahlinux.com 001.日常维护常用查询命令 #top 显示系统进程 #clear 清理屏幕信息 #cat /etc/redhat-r ...