Guava File操作

Stella981
• 阅读 890

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类中还提供了其他一些文件的简捷方法。比如

  1. touch方法创建或者更新文件的时间戳。

  2. createTempDir()方法创建临时目录

  3. Files.createParentDirs(File) 创建父级目录

  4. getChecksum(File)获得文件的checksum

  5. hash(File)获得文件的hash

  6. map系列方法获得文件的内存映射

  7. getFileExtension(String)获得文件的扩展名

  8. 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.tmpdirsystem 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.

点赞
收藏
评论区
推荐文章
blmius blmius
3年前
MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1
文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s
皕杰报表之UUID
​在我们用皕杰报表工具设计填报报表时,如何在新增行里自动增加id呢?能新增整数排序id吗?目前可以在新增行里自动增加id,但只能用uuid函数增加UUID编码,不能新增整数排序id。uuid函数说明:获取一个UUID,可以在填报表中用来创建数据ID语法:uuid()或uuid(sep)参数说明:sep布尔值,生成的uuid中是否包含分隔符'',缺省为
待兔 待兔
3个月前
手写Java HashMap源码
HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22
Jacquelyn38 Jacquelyn38
3年前
2020年前端实用代码段,为你的工作保驾护航
有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )
Stella981 Stella981
3年前
Android So动态加载 优雅实现与原理分析
背景:漫品Android客户端集成适配转换功能(基于目标识别(So库35M)和人脸识别库(5M)),导致apk体积50M左右,为优化客户端体验,决定实现So文件动态加载.!(https://oscimg.oschina.net/oscnet/00d1ff90e4b34869664fef59e3ec3fdd20b.png)点击上方“蓝字”关注我
Wesley13 Wesley13
3年前
mysql设置时区
mysql设置时区mysql\_query("SETtime\_zone'8:00'")ordie('时区设置失败,请联系管理员!');中国在东8区所以加8方法二:selectcount(user\_id)asdevice,CONVERT\_TZ(FROM\_UNIXTIME(reg\_time),'08:00','0
Wesley13 Wesley13
3年前
00:Java简单了解
浅谈Java之概述Java是SUN(StanfordUniversityNetwork),斯坦福大学网络公司)1995年推出的一门高级编程语言。Java是一种面向Internet的编程语言。随着Java技术在web方面的不断成熟,已经成为Web应用程序的首选开发语言。Java是简单易学,完全面向对象,安全可靠,与平台无关的编程语言。
Stella981 Stella981
3年前
Django中Admin中的一些参数配置
设置在列表中显示的字段,id为django模型默认的主键list_display('id','name','sex','profession','email','qq','phone','status','create_time')设置在列表可编辑字段list_editable
Wesley13 Wesley13
3年前
MySQL部分从库上面因为大量的临时表tmp_table造成慢查询
背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_
Python进阶者 Python进阶者
9个月前
Excel中这日期老是出来00:00:00,怎么用Pandas把这个去除
大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Pandas数据筛选的问题。问题如下:这日期老是出来00:00:00,怎么把这个去除。二、实现过程后来【论草莓如何成为冻干莓】给了一个思路和代码如下:pd.toexcel之前把这