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.允许上传文件数量 允许选择多个文件:< ...
随机推荐
- java开发--struts2 标签库使用
在工程中使用struts2标签 一.struts2标签定义文件在struts2-core-2.0.11.1\META-INF 下面,文件名为struts-tags.tld 二.如果工程使用了servl ...
- [iOS]ios archives 出现的是other items而不是iOS Apps的解决方案
百度了一上午, 总是这样子. (利用cocoapods上传项目的时候, 总是这样无法点击UpLoad to App Store) 然后. 这样做. 就可以了!
- Centos挂载windows共享文件夹
1.windows7共享一个文件夹(1)新建一个用户:devin,密码:admin123(2)在E盘新建一个文件夹,share,并设置共享 对用户devin共享,并让其权限为:读取和写入. 2.lin ...
- Java:包的使用Pack
在包A中创建一个类并在类中定义一个方法 package packA; public class PackDemoA { public void show() { System.out.println( ...
- 如何避免JSP页面自动生成session对象?为什么要这么做?
JSP // 在默认情况下,在对一个JSP页面发出请求时,如果session还没有建立,JSP页面会自动为请求建立一个session对象,但是session是比较消耗资源的,如果没必要保持和使用ses ...
- TEET
[{"PROCESS_STORE_TIME":"3min 11s","PROCESS_GET_FILE_TIME":"3min&q ...
- ADODB.Connection 错误 '800a0e7a'。。
今天帮同学调程序的时候发现的:错误提示如下: ADODB.Connection 错误 '800a0e7a' 未找到提供程序.该程序可能未正确安装. /hua1/manage/inc/conn.asp, ...
- 【linux】/dev/null与/dev/zero详解【转】
转自:http://www.cnblogs.com/xianghang123/archive/2012/03/23/2413381.html 使用/dev/null 把/dev/null 看作&quo ...
- Flex通过Blazeds利用Remoteservice与后台java消息推送
http://www.cnblogs.com/xia520pi/archive/2012/05/26/2519343.html http://computerdragon.blog.51cto.com ...
- Js判断一个单词是否有重复字母
今天上午刷到一道题,大体是写一个方法判断一个单词中是否有重复的字母(或者说一个字符串中是否有重复的字符).我的思路是一个字符一个字符地遍历,如果发现有重复的停止: function isIsogram ...