Java的基本API对文件的操作很繁琐,为了向文件中写入一行文本,都需要写十几行的代码。guava对此作了很多改进,提供了很多方便的操作。
一. Guava的文件写入
Guava的Files类中提供了几个write方法来简化向文件中写入内容的操作,下面的例子演示 Files.write(byte[],File)的用法。
import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;
import static com.google.common.base.Preconditions.checkNotNull;
/**
* Created by zhaoliangang on 15/6/26.
*/
public class GuavaFile {
public static 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) {
System.out.println("ERROR trying to write to file '" + fileName + "' - "
+ fileIoEx.toString());
}
}
public static void main(String[] args) {
GuavaFile.demoFileWrite("/Users/zhaoliangang/Documents/work/test.txt", "hello stefanie zhao");
}
}
二. 获得文件内容
Files类提供了readLines方法可以方便的读取文件的内容,如下demo代码:
public static void demoFileRead(final String filePath) throws IOException {
File testFile = new File(filePath);
List<String> lines = Files.readLines(testFile, Charsets.UTF_8);
for (String line : lines) {
System.out.println(line);
}
}
但是这个readLime方法是一次性读数据到内存,大文件当然就出现内存溢出了。
我们是用guava提供的另外一种readLine方法:
static <T> T
**[readLines](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23readLines%28java.io.File%2C+java.nio.charset.Charset%2C+com.google.common.io.LineProcessor%29)**([File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue) file, [Charset](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fnio%2Fcharset%2FCharset.html%3Fis-external%3Dtrue) charset, [LineProcessor](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FLineProcessor.html)<T> callback)
Streams lines from a File
, stopping when our callback returns false, or we have read all of the lines.
public static void demoFileReadAsyn(final String filePath) throws IOException {
File testFile = new File(filePath);
Integer rowNum = Files.readLines(testFile, Charsets.UTF_16, new LineProcessor<Integer>() {
private int rowNum = 0;
public boolean processLine(String s) throws IOException {
rowNum ++;
return true;
}
public Integer getResult() {
return rowNum;
}
});
System.out.println(rowNum);
}
这个readLines的重载,需要我们实现一个LineProcessor的泛型接口,在这个接口的实现方法processLine方法中我们可以对行文本进行处理,getResult方法可以获得一个最终的处理结果,这里我们只是简单的返回了一个行计数。
另外还有readBytes方法可以对文件的字节做处理,readFirstLine可以返回第一行的文本,Files.toString(File,Charset)可以返回文件的所有文本内容。
三. 复制移动(剪切)文件
final File sourceFile = new File(sourceFileName);
final File targetFile = new File(targetFileName);
Files.copy(sourceFile, targetFile);
四. 比较文件内容
Files.equal(file1, file2)
五. 其他有用的方法
Guava的Files类中还提供了其他一些文件的简捷方法。比如
touch方法创建或者更新文件的时间戳。
createTempDir()方法创建临时目录
Files.createParentDirs(File) 创建父级目录
getChecksum(File)获得文件的checksum
hash(File)获得文件的hash
map系列方法获得文件的内存映射
getFileExtension(String)获得文件的扩展名
getNameWithoutExtension(String file)获得不带扩展名的文件名
Guava的方法都提供了一些重载,这些重载可以扩展基本用法,我们也有必要去多了解一下,这些重载的方法。
附上Files类的doc:
static void
**[append](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23append%28java.lang.CharSequence%2C+java.io.File%2C+java.nio.charset.Charset%29)**([CharSequence](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Flang%2FCharSequence.html%3Fis-external%3Dtrue) from, [File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue) to, [Charset](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fnio%2Fcharset%2FCharset.html%3Fis-external%3Dtrue) charset)
Appends a character sequence (such as a string) to a file using the given character set.
static [ByteSink](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FByteSink.html)
**[asByteSink](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23asByteSink%28java.io.File%2C+com.google.common.io.FileWriteMode...%29)**([File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue) file, [FileWriteMode](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFileWriteMode.html)... modes)
Returns a new ByteSink
for writing bytes to the given file.
static [ByteSource](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FByteSource.html)
**[asByteSource](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23asByteSource%28java.io.File%29)**([File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue) file)
Returns a new ByteSource
for reading bytes from the given file.
static [CharSink](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FCharSink.html)
**[asCharSink](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23asCharSink%28java.io.File%2C+java.nio.charset.Charset%2C+com.google.common.io.FileWriteMode...%29)**([File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue) file, [Charset](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fnio%2Fcharset%2FCharset.html%3Fis-external%3Dtrue) charset, [FileWriteMode](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFileWriteMode.html)... modes)
Returns a new CharSink
for writing character data to the given file using the given character set.
static [CharSource](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FCharSource.html)
**[asCharSource](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23asCharSource%28java.io.File%2C+java.nio.charset.Charset%29)**([File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue) file, [Charset](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fnio%2Fcharset%2FCharset.html%3Fis-external%3Dtrue) charset)
Returns a new CharSource
for reading character data from the given file using the given character set.
static void
**[copy](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23copy%28java.io.File%2C+java.nio.charset.Charset%2C+java.lang.Appendable%29)**([File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue) from, [Charset](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fnio%2Fcharset%2FCharset.html%3Fis-external%3Dtrue) charset, [Appendable](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Flang%2FAppendable.html%3Fis-external%3Dtrue) to)
Copies all characters from a file to an appendable object, using the given character set.
static void
**[copy](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23copy%28java.io.File%2C+java.io.File%29)**([File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue) from, [File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue) to)
Copies all the bytes from one file to another.
static void
**[copy](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23copy%28java.io.File%2C+java.io.OutputStream%29)**([File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue) from, [OutputStream](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FOutputStream.html%3Fis-external%3Dtrue) to)
Copies all bytes from a file to an output stream.
static void
**[createParentDirs](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23createParentDirs%28java.io.File%29)**([File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue) file)
Creates any necessary but nonexistent parent directories of the specified file.
static [File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue)
**[createTempDir](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23createTempDir%28%29)**()
Atomically creates a new directory somewhere beneath the system's temporary directory (as defined by the java.io.tmpdir
system property), and returns its name.
static boolean
**[equal](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23equal%28java.io.File%2C+java.io.File%29)**([File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue) file1, [File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue) file2)
Returns true if the files contains the same bytes.
static [TreeTraverser](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fcollect%2FTreeTraverser.html)<[File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue)>
**[fileTreeTraverser](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23fileTreeTraverser%28%29)**()
Returns a TreeTraverser
instance for File
trees.
static [String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Flang%2FString.html%3Fis-external%3Dtrue)
**[getFileExtension](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23getFileExtension%28java.lang.String%29)**([String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Flang%2FString.html%3Fis-external%3Dtrue) fullName)
Returns the file extension for the given file name, or the empty string if the file has no extension.
static [String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Flang%2FString.html%3Fis-external%3Dtrue)
**[getNameWithoutExtension](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23getNameWithoutExtension%28java.lang.String%29)**([String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Flang%2FString.html%3Fis-external%3Dtrue) file)
Returns the file name without its file extension or path.
static [HashCode](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fhash%2FHashCode.html)
**[hash](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23hash%28java.io.File%2C+com.google.common.hash.HashFunction%29)**([File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue) file, [HashFunction](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fhash%2FHashFunction.html) hashFunction)
Computes the hash code of the file
using hashFunction
.
static [Predicate](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fbase%2FPredicate.html)<[File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue)>
**[isDirectory](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23isDirectory%28%29)**()
Returns a predicate that returns the result of File.isDirectory()
on input files.
static [Predicate](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fbase%2FPredicate.html)<[File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue)>
**[isFile](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23isFile%28%29)**()
Returns a predicate that returns the result of File.isFile()
on input files.
static [MappedByteBuffer](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fnio%2FMappedByteBuffer.html%3Fis-external%3Dtrue)
**[map](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23map%28java.io.File%29)**([File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue) file)
Fully maps a file read-only in to memory as per FileChannel.map(java.nio.channels.FileChannel.MapMode, long, long)
.
static [MappedByteBuffer](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fnio%2FMappedByteBuffer.html%3Fis-external%3Dtrue)
**[map](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23map%28java.io.File%2C+java.nio.channels.FileChannel.MapMode%29)**([File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue) file, [FileChannel.MapMode](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fnio%2Fchannels%2FFileChannel.MapMode.html%3Fis-external%3Dtrue) mode)
Fully maps a file in to memory as per FileChannel.map(java.nio.channels.FileChannel.MapMode, long, long)
using the requested FileChannel.MapMode
.
static [MappedByteBuffer](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fnio%2FMappedByteBuffer.html%3Fis-external%3Dtrue)
**[map](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23map%28java.io.File%2C+java.nio.channels.FileChannel.MapMode%2C+long%29)**([File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue) file, [FileChannel.MapMode](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fnio%2Fchannels%2FFileChannel.MapMode.html%3Fis-external%3Dtrue) mode, long size)
Maps a file in to memory as per FileChannel.map(java.nio.channels.FileChannel.MapMode, long, long)
using the requested FileChannel.MapMode
.
static void
**[move](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23move%28java.io.File%2C+java.io.File%29)**([File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue) from, [File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue) to)
Moves a file from one path to another.
static [BufferedReader](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FBufferedReader.html%3Fis-external%3Dtrue)
**[newReader](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23newReader%28java.io.File%2C+java.nio.charset.Charset%29)**([File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue) file, [Charset](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fnio%2Fcharset%2FCharset.html%3Fis-external%3Dtrue) charset)
Returns a buffered reader that reads from a file using the given character set.
static [BufferedWriter](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FBufferedWriter.html%3Fis-external%3Dtrue)
**[newWriter](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23newWriter%28java.io.File%2C+java.nio.charset.Charset%29)**([File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue) file, [Charset](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fnio%2Fcharset%2FCharset.html%3Fis-external%3Dtrue) charset)
Returns a buffered writer that writes to a file using the given character set.
static <T> T
**[readBytes](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23readBytes%28java.io.File%2C+com.google.common.io.ByteProcessor%29)**([File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue) file, [ByteProcessor](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FByteProcessor.html)<T> processor)
Process the bytes of a file.
static [String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Flang%2FString.html%3Fis-external%3Dtrue)
**[readFirstLine](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23readFirstLine%28java.io.File%2C+java.nio.charset.Charset%29)**([File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue) file, [Charset](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fnio%2Fcharset%2FCharset.html%3Fis-external%3Dtrue) charset)
Reads the first line from a file.
static [List](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Futil%2FList.html%3Fis-external%3Dtrue)<[String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Flang%2FString.html%3Fis-external%3Dtrue)>
**[readLines](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23readLines%28java.io.File%2C+java.nio.charset.Charset%29)**([File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue) file, [Charset](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fnio%2Fcharset%2FCharset.html%3Fis-external%3Dtrue) charset)
Reads all of the lines from a file.
static <T> T
**[readLines](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23readLines%28java.io.File%2C+java.nio.charset.Charset%2C+com.google.common.io.LineProcessor%29)**([File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue) file, [Charset](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fnio%2Fcharset%2FCharset.html%3Fis-external%3Dtrue) charset, [LineProcessor](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FLineProcessor.html)<T> callback)
Streams lines from a File
, stopping when our callback returns false, or we have read all of the lines.
static [String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Flang%2FString.html%3Fis-external%3Dtrue)
**[simplifyPath](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23simplifyPath%28java.lang.String%29)**([String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Flang%2FString.html%3Fis-external%3Dtrue) pathname)
Returns the lexically cleaned form of the path name, usually (but not always) equivalent to the original.
static byte[]
**[toByteArray](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23toByteArray%28java.io.File%29)**([File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue) file)
Reads all bytes from a file into a byte array.
static [String](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Flang%2FString.html%3Fis-external%3Dtrue)
**[toString](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23toString%28java.io.File%2C+java.nio.charset.Charset%29)**([File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue) file, [Charset](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fnio%2Fcharset%2FCharset.html%3Fis-external%3Dtrue) charset)
Reads all characters from a file into a String
, using the given character set.
static void
**[touch](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23touch%28java.io.File%29)**([File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue) file)
Creates an empty file or updates the last updated timestamp on the same as the unix command of the same name.
static void
**[write](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23write%28byte%5B%5D%2C+java.io.File%29)**(byte[] from, [File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue) to)
Overwrites a file with the contents of a byte array.
static void
**[write](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.guava-libraries.googlecode.com%2Fgit-history%2Frelease%2Fjavadoc%2Fcom%2Fgoogle%2Fcommon%2Fio%2FFiles.html%23write%28java.lang.CharSequence%2C+java.io.File%2C+java.nio.charset.Charset%29)**([CharSequence](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Flang%2FCharSequence.html%3Fis-external%3Dtrue) from, [File](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fio%2FFile.html%3Fis-external%3Dtrue) to, [Charset](https://www.oschina.net/action/GoToLink?url=http%3A%2F%2Fdocs.oracle.com%2Fjavase%2F7%2Fdocs%2Fapi%2Fjava%2Fnio%2Fcharset%2FCharset.html%3Fis-external%3Dtrue) charset)
Writes a character sequence (such as a string) to a file using the given character set.