guava-file文件操做java
今天看guava的file时看到了一个功能是建立临时文件夹,因而就多看了一些其余的关于file的操做API。ide
一. Guava的文件写入google
Guava的Files类中提供了几个write方法来简化向文件中写入内容的操做,下面的例子演示 Files.write(byte[],File)的用法。code
import static com.google.common.base.Preconditions.*; import static java.lang.System.*; import com.google.common.base.Charsets; import com.google.common.io.Files; //此处省略class /** * 演示向文件中写入字节流 * * @param fileName 要写入文件的文件名 * @param contents 要写入的文件内容 */ public void demoFileWrite(final String fileName, final String contents) { checkNotNull(fileName, "Provided file name for writing must NOT be null."); checkNotNull(contents, "Unable to write null contents."); final File newFile = new File(fileName); try { Files.write(contents.getBytes(), newFile); } catch (IOException fileIoEx) { err.println( "ERROR trying to write to file '" + fileName + "' - " + fileIoEx.toString()); } }
二.得到文件内容接口
Files类提供了readLines方法能够方便的读取文件的内容,以下demo代码:内存
String testFilePath = "d:\\test.txt"; File testFile = new File(testFilePath); List<String> lines = Files.readLines(testFile, Charsets.UTF_16); for (String line : lines) { System.out.println(line); }
注意这里的readLines方法返回的是List\<String>的接口,这对于大文件处理是会有问题的。大文件处理能够使用readLines方法的另外一个重载。下面的例子演示从一个大文件中逐行读取文本,并作行号计数。get
package main.com.jd.coo.guava.io; import java.io.File; import java.io.IOException; import com.google.common.base.Charsets; import com.google.common.io.Files; import com.google.common.io.LineProcessor; /** * Created by outofmemory.cn on 2014/7/24. */ public class FilesDemo { static class CounterLine implements LineProcessor<Integer> { private int rowNum = 0; @Override public boolean processLine(String line) throws IOException { rowNum ++; return true; } @Override public Integer getResult() { return rowNum; } } public static void main(String[] args) throws IOException { String testFilePath = "d:\\test.txt"; File testFile = new File(testFilePath); CounterLine counter = new CounterLine(); Files.readLines(testFile, Charsets.UTF_16, counter); System.out.println(counter.getResult()); } }
这个readLines的重载,须要咱们实现一个LineProcessor的泛型接口,在这个接口的实现方法processLine方法中咱们能够对行文本进行处理,getResult方法能够得到一个最终的处理结果,这里咱们只是简单的返回了一个行计数。hash
另外还有readBytes方法能够对文件的字节作处理,readFirstLine能够返回第一行的文本,Files.toString(File,Charset)能够返回文件的全部文本内容。it
三. 复制移动(剪切)文件io
在Guava中复制文件操做提供了一组的copy方法,咱们看一个示例:
/** * 演示如何使用guava的Files.copy方法复制文件 * * @param sourceFileName 复制的源文件名 * @param targetFileName 目标文件名 */ public void demoSimpleFileCopy( final String sourceFileName, final String targetFileName) { checkNotNull(sourceFileName, "Copy source file name must NOT be null."); checkNotNull(targetFileName, "Copy target file name must NOT be null."); final File sourceFile = new File(sourceFileName); final File targetFile = new File(targetFileName); try { Files.copy(sourceFile, targetFile); } catch (IOException fileIoEx) { err.println( "ERROR trying to copy file '" + sourceFileName + "' to file '" + targetFileName + "' - " + fileIoEx.toString()); } }
Guava中移动文件使用move方法,用法和copy同样。
四. 比较文件内容
Guava中提供了Files.equal(File,File)方法来比较两个文件的内容是否彻底一致,请看下面的示例:
/** * 演示 Files.equal(File,File) 来比较两个文件的内容 * * @param fileName1 比较的文件1文件名 * @param fileName2 比较的文件2文件名 */ public void demoEqual(final String fileName1, final String fileName2) { checkNotNull(fileName1, "First file name for comparison must NOT be null."); checkNotNull(fileName2, "Second file name for comparison must NOT be null."); final File file1 = new File(fileName1); final File file2 = new File(fileName2); try { out.println( "File '" + fileName1 + "' " + (Files.equal(file1, file2) ? "IS" : "is NOT") + " the same as file '" + fileName2 + "'."); } catch (IOException fileIoEx) { err.println( "ERROR trying to compare two files '" + fileName1 + "' and '" + fileName2 + "' - " + fileIoEx.toString()); } }
五. 其余有用的方法
Guava的Files类中还提供了其余一些文件的简捷方法。好比