Top Description Inners Fields Constructors Methods
com.google.common.io

public final Class Files

extends Object
Class Inheritance
Annotations
@J2ktIncompatible
@GwtIncompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkNotNull, com.google.common.io.FileWriteMode.APPEND

Provides utility methods for working with files.

java.nio.file.Path users will find similar utilities in MoreFiles and the JDK's java.nio.file.Files class.

Authors
Chris Nokleberg, Colin Decker
Since
1.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class
private static enum

Field Summary

Modifier and TypeField and Description
private static final SuccessorsFunction<File>

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static void
append(CharSequence
the character sequence to append
from
,
File
the destination file
to
,
Charset
the charset used to encode the output stream; see StandardCharsets for helpful predefined constants
charset
)

Deprecated Prefer asCharSink(to, charset, FileWriteMode.APPEND).write(from). This method is scheduled to be removed in October 2019.
Appends a character sequence (such as a string) to a file using the given character set.
public static ByteSink
asByteSink(File file, FileWriteMode... modes)

Returns a new ByteSink for writing bytes to the given file.

public static ByteSource
asByteSource(File file)

Returns a new ByteSource for reading bytes from the given file.

public static CharSink
asCharSink(File file, Charset charset, FileWriteMode... modes)

Returns a new CharSink for writing character data to the given file using the given character set.

public static CharSource
asCharSource(File file, Charset charset)

Returns a new CharSource for reading character data from the given file using the given character set.

public static void
copy(File
the source file
from
,
OutputStream
the output stream
to
)

Copies all bytes from a file to an output stream.

public static void
copy(File
the source file
from
,
File
the destination file
to
)

Copies all the bytes from one file to another.

public static void
copy(File
the source file
from
,
Charset
the charset used to decode the input stream; see StandardCharsets for helpful predefined constants
charset
,
Appendable
the appendable object
to
)

Deprecated Prefer asCharSource(from, charset).copyTo(to).
Copies all characters from a file to an appendable object, using the given character set.
public static void
createParentDirs(File file)

Creates any necessary but nonexistent parent directories of the specified file.

public static File

Returns:

the newly-created directory
createTempDir
()

Deprecated For Android users, see the Data and File Storage overview to select an appropriate temporary directory (perhaps context.getCacheDir()), and create your own directory under that.
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.
public static boolean
equal(File file1, File file2)

Returns true if the given files exist, are not directories, and contain the same bytes.

public static Traverser<File>
fileTraverser()

Returns a Traverser instance for the file and directory tree.

public static String
getFileExtension(String fullName)

Returns the file extension for the given file name, or the empty string if the file has no extension.

public static String

Returns:

The file name without its path or extension.
getNameWithoutExtension
(String
The name of the file to trim the extension from. This can be either a fully qualified file name (including a path) or just a file name.
file
)

Returns the file name without its file extension or path.

public static HashCode

Returns:

the HashCode of all of the bytes in the file
hash
(File
the file to read
file
,
HashFunction
the hash function to use to hash the data
hashFunction
)

Deprecated Prefer asByteSource(file).hash(hashFunction).
Computes the hash code of the file using hashFunction.
public static Predicate<File>
isDirectory()

Returns a predicate that returns the result of File#isDirectory on input files.

public static Predicate<File>
isFile()

Returns a predicate that returns the result of File#isFile on input files.

public static MappedByteBuffer

Returns:

a read-only buffer reflecting file
map
(File
the file to map
file
)

Fully maps a file read-only in to memory as per FileChannel#map(java.nio.channels.FileChannel.MapMode, long, long).

public static MappedByteBuffer

Returns:

a buffer reflecting file
map
(File
the file to map
file
,
FileChannel.MapMode
the mode to use when mapping file
mode
)

Fully maps a file in to memory as per FileChannel#map(java.nio.channels.FileChannel.MapMode, long, long) using the requested MapMode.

public static MappedByteBuffer

Returns:

a buffer reflecting file
map
(File
the file to map
file
,
FileChannel.MapMode
the mode to use when mapping file
mode
,
long size)

Maps a file in to memory as per FileChannel#map(java.nio.channels.FileChannel.MapMode, long, long) using the requested MapMode.

private static MappedByteBuffer
mapInternal(File file, FileChannel.MapMode mode, long size)

public static void
move(File
the source file
from
,
File
the destination file
to
)

Moves a file from one path to another.

public static BufferedReader

Returns:

the buffered reader
newReader
(File
the file to read from
file
,
Charset
the charset used to decode the input stream; see StandardCharsets for helpful predefined constants
charset
)

Returns a buffered reader that reads from a file using the given character set.

public static BufferedWriter

Returns:

the buffered writer
newWriter
(File
the file to write to
file
,
Charset
the charset used to encode the output stream; see StandardCharsets for helpful predefined constants
charset
)

Returns a buffered writer that writes to a file using the given character set.

public static <T extends @Nullable Object> T

Returns:

the result of the byte processor
readBytes
(File
the file to read
file
,
ByteProcessor<T>
the object to which the bytes of the file are passed.
processor
)

Deprecated Prefer asByteSource(file).read(processor).
Process the bytes of a file.
public static String

Returns:

the first line, or null if the file is empty
readFirstLine
(File
the file to read from
file
,
Charset
the charset used to decode the input stream; see StandardCharsets for helpful predefined constants
charset
)

Deprecated Prefer asCharSource(file, charset).readFirstLine().
Reads the first line from a file.
public static List<String>

Returns:

a mutable List containing all the lines
readLines
(File
the file to read from
file
,
Charset
the charset used to decode the input stream; see StandardCharsets for helpful predefined constants
charset
)

Reads all of the lines from a file.

public static <T extends @Nullable Object> T

Returns:

the output of processing the lines
readLines
(File
the file to read from
file
,
Charset
the charset used to decode the input stream; see StandardCharsets for helpful predefined constants
charset
,
LineProcessor<T>
the LineProcessor to use to handle the lines
callback
)

Deprecated Prefer asCharSource(file, charset).readLines(callback).
Streams lines from a File, stopping when our callback returns false, or we have read all of the lines.
public static String
simplifyPath(String pathname)

Returns the lexically cleaned form of the path name, usually (but not always) equivalent to the original.

public static byte[]

Returns:

a byte array containing all the bytes from file
toByteArray
(File
the file to read from
file
)

Reads all bytes from a file into a byte array.

public static String

Returns:

a string containing all the characters from the file
toString
(File
the file to read from
file
,
Charset
the charset used to decode the input stream; see StandardCharsets for helpful predefined constants
charset
)

Deprecated Prefer asCharSource(file, charset).read().
Reads all characters from a file into a String, using the given character set.
public static void
touch(File
the file to create or update
file
)

Creates an empty file or updates the last updated timestamp on the same as the unix command of the same name.

public static void
write(byte[]
the bytes to write
from
,
File
the destination file
to
)

Overwrites a file with the contents of a byte array.

public static void
write(CharSequence
the character sequence to write
from
,
File
the destination file
to
,
Charset
the charset used to encode the output stream; see StandardCharsets for helpful predefined constants
charset
)

Deprecated Prefer asCharSink(to, charset).write(from).
Writes a character sequence (such as a string) to a file using the given character set.
Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait