Files

write

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

write

1  public static void write(byte[] from, File to) throws IOException {
2         asByteSink(to, new FileWriteMode[0]).write(from);
3     }

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

readLines

 1 public static List<String> readLines(File file, Charset charset) throws IOException {
 2         return (List)readLines(file, charset, new LineProcessor() {
 3             final List<String> result = Lists.newArrayList();
 4
 5             public boolean processLine(String line) {
 6                 this.result.add(line);
 7                 return true;
 8             }
 9
10             public List<String> getResult() {
11                 return this.result;
12             }
13         });
14     }
15
16     public static <T> T readLines(File file, Charset charset, LineProcessor<T> callback) throws IOException {
17         return asCharSource(file, charset).readLines(callback);
18     }

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

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

equal

 1  public static boolean equal(File file1, File file2) throws IOException {
 2         Preconditions.checkNotNull(file1);
 3         Preconditions.checkNotNull(file2);
 4         if(file1 != file2 && !file1.equals(file2)) {
 5             long len1 = file1.length();
 6             long len2 = file2.length();
 7             return len1 != 0L && len2 != 0L && len1 != len2?false:asByteSource(file1).contentEquals(asByteSource(file2));
 8         } else {
 9             return true;
10         }
11     }

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

时间: 2024-12-15 08:59:30

Files的相关文章

出现unmapped spring configuration files found

intell idea启动出现unmapped spring configuration files found提示. 把spring里面的内容都打勾.

My first makefile to compile multiple C files

I have three files to compile: main.c, func.c,  func.h The steps: 1   main.c   to   main.o 2   func.c    to   func.o 3    link main.o func.o to main(file that can execute) So, u need to run at least three commands without a Makefile.Then if u have 10

没有躲过的坑--你开发的软件安装在C盘Program Files (x86)下产生的异常

今天偶然发现的问题,就是自己写的win32程序安装在C盘Program Files (x86)文件夹下就会产生异常,而安装在其他文件夹,即使是D盘的Program Files (x86)下,程序也可以完美运行. 引起这个,肯定是权限的问题.这个软件运行时,需要读写数据库,以及各种缓存数据. 这就是我给自己挖的一个坑儿,相信很多人也会遇到这个问题. 就是在开发程序.VS调试的时候,总喜欢使用当前路径,或者是相对路径.即把一些数据与.exe文件放在一起. 但是安装的时候,选择的是C盘Program

【Android-tips】 Unable to execute dex: Multiple dex files define 解决方法

唔,之前已经想过今后不动android,没想到还是因为比赛的原因重操旧业.android有很多问题是由于eclipse的不完善造成的,比如今天遇到的这个问题 Unable to execute dex: Multiple dex files define [2011-10-23 16:23:29 - Dex Loader] Unable to execute dex: Multiple dex files define Lcom/myapp/R$array; [2011-10-23 16:23:

【分享】利用Apache的Htaccess Files命令限制访问文件类型,Files正则

如果你在你的模板文件夹中有很多PSD HTML模板,那么用接下来这个htaccess文件可以保护限制访问: 文件D:\WebSite\ZBPHP.COM\www\Tpl\.htaccess 全部源码如下: <Files ~ "\.(html?|tpl|psd|zip|rar)$"> Order Allow,Deny Deny from all </Files> [分享]利用Apache的Htaccess Files命令限制访问文件类型,Files正则,布布扣,b

Advanced Installer读取注册表时将Program Files读取为Program Files (x86)的解决办法

原文:Advanced Installer读取注册表时将Program Files读取为Program Files (x86)的解决办法 今天同事在做安装包的时候,有一个读取注册表路径的需求,需要根据读取的值来写配置文件,按照常规的做法,写好了注册表搜索方法,但是在测试的时候,发现总是会将系统盘下的Program Files\xxx路径读取为Program Files (x86)\xxx,如下图所示: 之后测试了如果读取非系统盘下的此路径,不会出现这个问题. 但是这个路径一般情况下都是默认安装在

DB太大?一键帮你收缩所有DB文件大小(Shrink Files for All Databases in SQL Server)

本文介绍一个简单的SQL脚本,实现收缩整个Microsoft SQL Server实例所有非系统DB文件大小的功能. 作为一个与SQL天天打交道的程序猿,经常会遇到DB文件太大,把空间占满的情况: 而对于开发测试人员来说,如果DB数据不是特别重要的话,不会特意扩大磁盘空间,而是直接利用SQL的Shrink File功能缩小DB文件大小,详见:https://docs.microsoft.com/en-us/sql/relational-databases/databases/shrink-a-f

The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files

问题描述 Exception in thread "main" java.lang.Error: Unresolved compilation problems: The type java.lang.CharSequence cannot be resolved. It is indirectly referenced from required .class files The method getText() from the type TagNode refers to the

测并发 Too many open files 问题的解决

ulimit -a 查看限制显示: core file size (blocks, -c) 0 data seg size (kbytes, -d) unlimited scheduling priority (-e) 0 file size (blocks, -f) unlimited pending signals (-i) 23256 max locked memory (kbytes, -l) 64 max memory size (kbytes, -m) unlimited open

deploy: [mkdir] Created dir: C:\Program Files\Java\apache-cxf-2.4.2\samples\java_first_pojo\build [loadfile] Do not set property srcbuild.classpath as its length is 0.

使用CXF的错误,错误是说我的路径有错误,因为路径错误所以无法运行程序 (1)原因,我将其放入了Program Files文件夹下,所以,其不好使 分析原因: 目录路径错误,目录中不能有空格,否则其解析器无法解析 解决方法: 更改使用的目录,改为C:\apache-cxf-2.4.2\samples\java_first_pojo问题解决 (2)另一个问题.Apache CXF requires Java versio n 1.5 or higher. You are currently usi