@Beta
public final class Files
extends java.lang.Object
All method parameters must be non-null unless documented otherwise.
| Modifier and Type | Method and Description |
|---|---|
static void |
append(java.lang.CharSequence from,
java.io.File to,
java.nio.charset.Charset charset)
Appends a character sequence (such as a string) to a file using the given
character set.
|
static ByteSink |
asByteSink(java.io.File file,
FileWriteMode... modes)
Returns a new
ByteSink for writing bytes to the given file. |
static ByteSource |
asByteSource(java.io.File file)
Returns a new
ByteSource for reading bytes from the given file. |
static CharSink |
asCharSink(java.io.File file,
java.nio.charset.Charset charset,
FileWriteMode... modes)
Returns a new
CharSink for writing character data to the given
file using the given character set. |
static CharSource |
asCharSource(java.io.File file,
java.nio.charset.Charset charset)
Returns a new
CharSource for reading character data from the given
file using the given character set. |
static void |
copy(java.io.File from,
java.nio.charset.Charset charset,
java.lang.Appendable to)
Copies all characters from a file to an appendable object,
using the given character set.
|
static <W extends java.lang.Appendable & java.io.Closeable> |
copy(java.io.File from,
java.nio.charset.Charset charset,
OutputSupplier<W> to)
Deprecated.
Use
Files.asCharSource(from, charset).copyTo(to) after
changing to to a CharSink if necessary. This method is
scheduled to be removed in Guava 18.0. |
static void |
copy(java.io.File from,
java.io.File to)
Copies all the bytes from one file to another.
|
static void |
copy(java.io.File from,
java.io.OutputStream to)
Copies all bytes from a file to an output stream.
|
static void |
copy(java.io.File from,
OutputSupplier<? extends java.io.OutputStream> to)
Deprecated.
Use
Files.asByteSource(from).copyTo(to) after changing
to to a ByteSink if necessary. This method is
scheduled to be removed in Guava 18.0. |
static void |
copy(InputSupplier<? extends java.io.InputStream> from,
java.io.File to)
Deprecated.
Use
from.copyTo(Files.asByteSink(to)) after changing
from to a ByteSource if necessary. This method is
scheduled to be removed in Guava 18.0. |
static <R extends java.lang.Readable & java.io.Closeable> |
copy(InputSupplier<R> from,
java.io.File to,
java.nio.charset.Charset charset)
Deprecated.
Use
from.copyTo(Files.asCharSink(to, charset)) after
changing from to a CharSource if necessary. This
method is scheduled to be removed in Guava 18.0. |
static void |
createParentDirs(java.io.File file)
Creates any necessary but nonexistent parent directories of the specified
file.
|
static java.io.File |
createTempDir()
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(java.io.File file1,
java.io.File file2)
Returns true if the files contains the same bytes.
|
static net.tribe7.common.collect.TreeTraverser<java.io.File> |
fileTreeTraverser()
Returns a
TreeTraverser instance for File trees. |
static java.lang.String |
getFileExtension(java.lang.String fullName)
Returns the file
extension for the given file name, or the empty string if the file has
no extension.
|
static java.lang.String |
getNameWithoutExtension(java.lang.String file)
Returns the file name without its
file extension or path.
|
static net.tribe7.common.hash.HashCode |
hash(java.io.File file,
net.tribe7.common.hash.HashFunction hashFunction)
Computes the hash code of the
file using hashFunction. |
static net.tribe7.common.base.Predicate<java.io.File> |
isDirectory()
Returns a predicate that returns the result of
File.isDirectory() on input files. |
static net.tribe7.common.base.Predicate<java.io.File> |
isFile()
Returns a predicate that returns the result of
File.isFile() on input files. |
static java.nio.MappedByteBuffer |
map(java.io.File file)
Fully maps a file read-only in to memory as per
FileChannel.map(java.nio.channels.FileChannel.MapMode, long, long). |
static java.nio.MappedByteBuffer |
map(java.io.File file,
java.nio.channels.FileChannel.MapMode 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 java.nio.MappedByteBuffer |
map(java.io.File file,
java.nio.channels.FileChannel.MapMode 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(java.io.File from,
java.io.File to)
Moves the file from one path to another.
|
static InputSupplier<java.io.FileInputStream> |
newInputStreamSupplier(java.io.File file)
Deprecated.
Use
asByteSource(File). This method is scheduled for
removal in Guava 18.0. |
static OutputSupplier<java.io.FileOutputStream> |
newOutputStreamSupplier(java.io.File file)
Deprecated.
Use
#asByteSink(File). This method is scheduled for
removal in Guava 18.0. |
static OutputSupplier<java.io.FileOutputStream> |
newOutputStreamSupplier(java.io.File file,
boolean append)
Deprecated.
Use
asByteSink(File, FileWriteMode...), passing
FileWriteMode.APPEND for append. This method is scheduled for
removal in Guava 18.0. |
static java.io.BufferedReader |
newReader(java.io.File file,
java.nio.charset.Charset charset)
Returns a buffered reader that reads from a file using the given
character set.
|
static InputSupplier<java.io.InputStreamReader> |
newReaderSupplier(java.io.File file,
java.nio.charset.Charset charset)
Deprecated.
Use
asCharSource(File, Charset). This method is
scheduled for removal in Guava 18.0. |
static java.io.BufferedWriter |
newWriter(java.io.File file,
java.nio.charset.Charset charset)
Returns a buffered writer that writes to a file using the given
character set.
|
static OutputSupplier<java.io.OutputStreamWriter> |
newWriterSupplier(java.io.File file,
java.nio.charset.Charset charset)
Deprecated.
Use
#asCharSink(File, Charset). This method is
scheduled for removal in Guava 18.0. |
static OutputSupplier<java.io.OutputStreamWriter> |
newWriterSupplier(java.io.File file,
java.nio.charset.Charset charset,
boolean append)
Deprecated.
Use
asCharSink(File, Charset, FileWriteMode...),
passing FileWriteMode.APPEND for append. This method is
scheduled for removal in Guava 18.0. |
static <T> T |
readBytes(java.io.File file,
ByteProcessor<T> processor)
Process the bytes of a file.
|
static java.lang.String |
readFirstLine(java.io.File file,
java.nio.charset.Charset charset)
Reads the first line from a file.
|
static java.util.List<java.lang.String> |
readLines(java.io.File file,
java.nio.charset.Charset charset)
Reads all of the lines from a file.
|
static <T> T |
readLines(java.io.File file,
java.nio.charset.Charset charset,
LineProcessor<T> callback)
Streams lines from a
File, stopping when our callback returns
false, or we have read all of the lines. |
static java.lang.String |
simplifyPath(java.lang.String pathname)
Returns the lexically cleaned form of the path name, usually (but
not always) equivalent to the original.
|
static byte[] |
toByteArray(java.io.File file)
Reads all bytes from a file into a byte array.
|
static java.lang.String |
toString(java.io.File file,
java.nio.charset.Charset charset)
Reads all characters from a file into a
String, using the given
character set. |
static void |
touch(java.io.File 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(byte[] from,
java.io.File to)
Overwrites a file with the contents of a byte array.
|
static void |
write(java.lang.CharSequence from,
java.io.File to,
java.nio.charset.Charset charset)
Writes a character sequence (such as a string) to a file using the given
character set.
|
public static java.io.BufferedReader newReader(java.io.File file,
java.nio.charset.Charset charset)
throws java.io.FileNotFoundException
file - the file to read fromcharset - the charset used to decode the input stream; see Charsets for helpful predefined constantsjava.io.FileNotFoundExceptionpublic static java.io.BufferedWriter newWriter(java.io.File file,
java.nio.charset.Charset charset)
throws java.io.FileNotFoundException
file - the file to write tocharset - the charset used to encode the output stream; see Charsets for helpful predefined constantsjava.io.FileNotFoundExceptionpublic static ByteSource asByteSource(java.io.File file)
ByteSource for reading bytes from the given file.public static ByteSink asByteSink(java.io.File file, FileWriteMode... modes)
ByteSink for writing bytes to the given file. The
given modes control how the file is opened for writing. When no
mode is provided, the file will be truncated before writing. When the
APPEND mode is provided, writes will
append to the end of the file without truncating it.public static CharSource asCharSource(java.io.File file, java.nio.charset.Charset charset)
CharSource for reading character data from the given
file using the given character set.public static CharSink asCharSink(java.io.File file, java.nio.charset.Charset charset, FileWriteMode... modes)
CharSink for writing character data to the given
file using the given character set. The given modes control how
the file is opened for writing. When no mode is provided, the file
will be truncated before writing. When the
APPEND mode is provided, writes will
append to the end of the file without truncating it.@Deprecated public static InputSupplier<java.io.FileInputStream> newInputStreamSupplier(java.io.File file)
asByteSource(File). This method is scheduled for
removal in Guava 18.0.FileInputStream
that read from a file.file - the file to read from@Deprecated public static OutputSupplier<java.io.FileOutputStream> newOutputStreamSupplier(java.io.File file)
#asByteSink(File). This method is scheduled for
removal in Guava 18.0.FileOutputStream
that write to a file.file - the file to write to@Deprecated public static OutputSupplier<java.io.FileOutputStream> newOutputStreamSupplier(java.io.File file, boolean append)
asByteSink(File, FileWriteMode...), passing
FileWriteMode.APPEND for append. This method is scheduled for
removal in Guava 18.0.FileOutputStream
that write to or append to a file.file - the file to write toappend - if true, the encoded characters will be appended to the file;
otherwise the file is overwritten@Deprecated public static InputSupplier<java.io.InputStreamReader> newReaderSupplier(java.io.File file, java.nio.charset.Charset charset)
asCharSource(File, Charset). This method is
scheduled for removal in Guava 18.0.InputStreamReader that read a file using the given character set.file - the file to read fromcharset - the charset used to decode the input stream; see Charsets for helpful predefined constants@Deprecated public static OutputSupplier<java.io.OutputStreamWriter> newWriterSupplier(java.io.File file, java.nio.charset.Charset charset)
#asCharSink(File, Charset). This method is
scheduled for removal in Guava 18.0.OutputStreamWriter
that write to a file using the given character set.file - the file to write tocharset - the charset used to encode the output stream; see Charsets for helpful predefined constants@Deprecated public static OutputSupplier<java.io.OutputStreamWriter> newWriterSupplier(java.io.File file, java.nio.charset.Charset charset, boolean append)
asCharSink(File, Charset, FileWriteMode...),
passing FileWriteMode.APPEND for append. This method is
scheduled for removal in Guava 18.0.OutputStreamWriter
that write to or append to a file using the given character set.file - the file to write tocharset - the charset used to encode the output stream; see Charsets for helpful predefined constantsappend - if true, the encoded characters will be appended to the file;
otherwise the file is overwrittenpublic static byte[] toByteArray(java.io.File file)
throws java.io.IOException
file - the file to read fromjava.lang.IllegalArgumentException - if the file is bigger than the largest
possible byte array (2^31 - 1)java.io.IOException - if an I/O error occurspublic static java.lang.String toString(java.io.File file,
java.nio.charset.Charset charset)
throws java.io.IOException
String, using the given
character set.file - the file to read fromcharset - the charset used to decode the input stream; see Charsets for helpful predefined constantsjava.io.IOException - if an I/O error occurs@Deprecated public static void copy(InputSupplier<? extends java.io.InputStream> from, java.io.File to) throws java.io.IOException
from.copyTo(Files.asByteSink(to)) after changing
from to a ByteSource if necessary. This method is
scheduled to be removed in Guava 18.0.InputStream supplied by a
factory.from - the input factoryto - the destination filejava.io.IOException - if an I/O error occurspublic static void write(byte[] from,
java.io.File to)
throws java.io.IOException
from - the bytes to writeto - the destination filejava.io.IOException - if an I/O error occurs@Deprecated
public static void copy(java.io.File from,
OutputSupplier<? extends java.io.OutputStream> to)
throws java.io.IOException
Files.asByteSource(from).copyTo(to) after changing
to to a ByteSink if necessary. This method is
scheduled to be removed in Guava 18.0.OutputStream supplied by
a factory.from - the source fileto - the output factoryjava.io.IOException - if an I/O error occurspublic static void copy(java.io.File from,
java.io.OutputStream to)
throws java.io.IOException
from - the source fileto - the output streamjava.io.IOException - if an I/O error occurspublic static void copy(java.io.File from,
java.io.File to)
throws java.io.IOException
Warning: If to represents an existing file, that file
will be overwritten with the contents of from. If to and
from refer to the same file, the contents of that file
will be deleted.
from - the source fileto - the destination filejava.io.IOException - if an I/O error occursjava.lang.IllegalArgumentException - if from.equals(to)@Deprecated public static <R extends java.lang.Readable & java.io.Closeable> void copy(InputSupplier<R> from, java.io.File to, java.nio.charset.Charset charset) throws java.io.IOException
from.copyTo(Files.asCharSink(to, charset)) after
changing from to a CharSource if necessary. This
method is scheduled to be removed in Guava 18.0.Readable and
Closeable object supplied by a factory, using the given
character set.from - the readable supplierto - the destination filecharset - the charset used to encode the output stream; see Charsets for helpful predefined constantsjava.io.IOException - if an I/O error occurspublic static void write(java.lang.CharSequence from,
java.io.File to,
java.nio.charset.Charset charset)
throws java.io.IOException
from - the character sequence to writeto - the destination filecharset - the charset used to encode the output stream; see Charsets for helpful predefined constantsjava.io.IOException - if an I/O error occurspublic static void append(java.lang.CharSequence from,
java.io.File to,
java.nio.charset.Charset charset)
throws java.io.IOException
from - the character sequence to appendto - the destination filecharset - the charset used to encode the output stream; see Charsets for helpful predefined constantsjava.io.IOException - if an I/O error occurs@Deprecated
public static <W extends java.lang.Appendable & java.io.Closeable> void copy(java.io.File from,
java.nio.charset.Charset charset,
OutputSupplier<W> to)
throws java.io.IOException
Files.asCharSource(from, charset).copyTo(to) after
changing to to a CharSink if necessary. This method is
scheduled to be removed in Guava 18.0.Appendable &
Closeable object supplied by a factory, using the given
character set.from - the source filecharset - the charset used to decode the input stream; see Charsets for helpful predefined constantsto - the appendable supplierjava.io.IOException - if an I/O error occurspublic static void copy(java.io.File from,
java.nio.charset.Charset charset,
java.lang.Appendable to)
throws java.io.IOException
from - the source filecharset - the charset used to decode the input stream; see Charsets for helpful predefined constantsto - the appendable objectjava.io.IOException - if an I/O error occurspublic static boolean equal(java.io.File file1,
java.io.File file2)
throws java.io.IOException
java.io.IOException - if an I/O error occurspublic static java.io.File createTempDir()
java.io.tmpdir system
property), and returns its name.
Use this method instead of File.createTempFile(String, String)
when you wish to create a directory, not a regular file. A common pitfall
is to call createTempFile, delete the file and create a
directory in its place, but this leads a race condition which can be
exploited to create security vulnerabilities, especially when executable
files are to be written into the directory.
This method assumes that the temporary volume is writable, has free inodes and free blocks, and that it will not be called thousands of times per second.
java.lang.IllegalStateException - if the directory could not be createdpublic static void touch(java.io.File file)
throws java.io.IOException
file - the file to create or updatejava.io.IOException - if an I/O error occurspublic static void createParentDirs(java.io.File file)
throws java.io.IOException
java.io.IOException - if an I/O error occurs, or if any necessary but
nonexistent parent directories of the specified file could not be
created.public static void move(java.io.File from,
java.io.File to)
throws java.io.IOException
mv command.from - the source fileto - the destination filejava.io.IOException - if an I/O error occursjava.lang.IllegalArgumentException - if from.equals(to)public static java.lang.String readFirstLine(java.io.File file,
java.nio.charset.Charset charset)
throws java.io.IOException
file - the file to read fromcharset - the charset used to decode the input stream; see Charsets for helpful predefined constantsjava.io.IOException - if an I/O error occurspublic static java.util.List<java.lang.String> readLines(java.io.File file,
java.nio.charset.Charset charset)
throws java.io.IOException
This method returns a mutable List. For an
ImmutableList, use
Files.asCharSource(file, charset).readLines().
file - the file to read fromcharset - the charset used to decode the input stream; see Charsets for helpful predefined constantsList containing all the linesjava.io.IOException - if an I/O error occurspublic static <T> T readLines(java.io.File file,
java.nio.charset.Charset charset,
LineProcessor<T> callback)
throws java.io.IOException
File, stopping when our callback returns
false, or we have read all of the lines.file - the file to read fromcharset - the charset used to decode the input stream; see Charsets for helpful predefined constantscallback - the LineProcessor to use to handle the linesjava.io.IOException - if an I/O error occurspublic static <T> T readBytes(java.io.File file,
ByteProcessor<T> processor)
throws java.io.IOException
(If this seems too complicated, maybe you're looking for
toByteArray(java.io.File).)
file - the file to readprocessor - the object to which the bytes of the file are passed.java.io.IOException - if an I/O error occurspublic static net.tribe7.common.hash.HashCode hash(java.io.File file,
net.tribe7.common.hash.HashFunction hashFunction)
throws java.io.IOException
file using hashFunction.file - the file to readhashFunction - the hash function to use to hash the dataHashCode of all of the bytes in the filejava.io.IOException - if an I/O error occurspublic static java.nio.MappedByteBuffer map(java.io.File file)
throws java.io.IOException
FileChannel.map(java.nio.channels.FileChannel.MapMode, long, long).
Files are mapped from offset 0 to its length.
This only works for files <= Integer.MAX_VALUE bytes.
file - the file to mapfilejava.io.FileNotFoundException - if the file does not existjava.io.IOException - if an I/O error occursFileChannel.map(MapMode, long, long)public static java.nio.MappedByteBuffer map(java.io.File file,
java.nio.channels.FileChannel.MapMode mode)
throws java.io.IOException
FileChannel.map(java.nio.channels.FileChannel.MapMode, long, long)
using the requested FileChannel.MapMode.
Files are mapped from offset 0 to its length.
This only works for files <= Integer.MAX_VALUE bytes.
file - the file to mapmode - the mode to use when mapping filefilejava.io.FileNotFoundException - if the file does not existjava.io.IOException - if an I/O error occursFileChannel.map(MapMode, long, long)public static java.nio.MappedByteBuffer map(java.io.File file,
java.nio.channels.FileChannel.MapMode mode,
long size)
throws java.io.FileNotFoundException,
java.io.IOException
FileChannel.map(java.nio.channels.FileChannel.MapMode, long, long)
using the requested FileChannel.MapMode.
Files are mapped from offset 0 to size.
If the mode is FileChannel.MapMode.READ_WRITE and the file does not exist,
it will be created with the requested size. Thus this method is
useful for creating memory mapped files which do not yet exist.
This only works for files <= Integer.MAX_VALUE bytes.
file - the file to mapmode - the mode to use when mapping filefilejava.io.IOException - if an I/O error occursjava.io.FileNotFoundExceptionFileChannel.map(MapMode, long, long)public static java.lang.String simplifyPath(java.lang.String pathname)
These heuristics do not always match the behavior of the filesystem. In
particular, consider the path a/../b, which simplifyPath
will change to b. If a is a symlink to x, a/../b may refer to a sibling of x, rather than the sibling of
a referred to by b.
public static java.lang.String getFileExtension(java.lang.String fullName)
.'.public static java.lang.String getNameWithoutExtension(java.lang.String file)
basename unix command. The result does not include the '.'.file - 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.public static net.tribe7.common.collect.TreeTraverser<java.io.File> fileTreeTraverser()
TreeTraverser instance for File trees.
Warning: File provides no support for symbolic links, and as such there is no
way to ensure that a symbolic link to a directory is not followed when traversing the tree.
In this case, iterables created by this traverser could contain files that are outside of the
given directory or even be infinite if there is a symbolic link loop.
public static net.tribe7.common.base.Predicate<java.io.File> isDirectory()
File.isDirectory() on input files.public static net.tribe7.common.base.Predicate<java.io.File> isFile()
File.isFile() on input files.