|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||
java.lang.Objectcom.google.common.io.Files
@Beta public final class Files
Provides utility methods for working with files.
All method parameters must be non-null unless documented otherwise.
| Method Summary | ||
|---|---|---|
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 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
|
copy(java.io.File from,
java.nio.charset.Charset charset,
OutputSupplier<W> to)
Copies all characters from a file to a Appendable &
Closeable object supplied by a factory, using the given
character set. |
|
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)
Copies all bytes from a file to an OutputStream supplied by
a factory. |
|
static void |
copy(InputSupplier<? extends java.io.InputStream> from,
java.io.File to)
Copies to a file all bytes from an InputStream supplied by a
factory. |
|
static
|
copy(InputSupplier<R> from,
java.io.File to,
java.nio.charset.Charset charset)
Copies to a file all characters from a Readable and
Closeable object supplied by a factory, using the given
character set. |
|
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 void |
deleteDirectoryContents(java.io.File directory)
Deletes all the files within a directory. |
|
static void |
deleteRecursively(java.io.File file)
Deletes a file or directory and all contents recursively. |
|
static boolean |
equal(java.io.File file1,
java.io.File file2)
Returns true if the files contains the same bytes. |
|
static long |
getChecksum(java.io.File file,
java.util.zip.Checksum checksum)
Computes and returns the checksum value for a file. |
|
static byte[] |
getDigest(java.io.File file,
java.security.MessageDigest md)
Computes and returns the digest value for a file. |
|
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)
Returns a factory that will supply instances of FileInputStream
that read from a file. |
|
static OutputSupplier<java.io.FileOutputStream> |
newOutputStreamSupplier(java.io.File file)
Returns a factory that will supply instances of FileOutputStream
that write to a file. |
|
static OutputSupplier<java.io.FileOutputStream> |
newOutputStreamSupplier(java.io.File file,
boolean append)
Returns a factory that will supply instances of FileOutputStream
that write to or append to a file. |
|
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)
Returns a factory that will supply instances of InputStreamReader that read a file using the given character set. |
|
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)
Returns a factory that will supply instances of OutputStreamWriter
that write to a file using the given character set. |
|
static OutputSupplier<java.io.OutputStreamWriter> |
newWriterSupplier(java.io.File file,
java.nio.charset.Charset charset,
boolean append)
Returns a factory that will supply instances of OutputStreamWriter
that write to or append to a file using the given character set. |
|
static
|
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
|
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 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. |
|
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Method Detail |
|---|
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 character set used when writing the file
java.io.FileNotFoundException
public 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 character set used when writing the file
java.io.FileNotFoundExceptionpublic static InputSupplier<java.io.FileInputStream> newInputStreamSupplier(java.io.File file)
FileInputStream
that read from a file.
file - the file to read from
public static OutputSupplier<java.io.FileOutputStream> newOutputStreamSupplier(java.io.File file)
FileOutputStream
that write to a file.
file - the file to write to
public static OutputSupplier<java.io.FileOutputStream> newOutputStreamSupplier(java.io.File file,
boolean append)
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
public static InputSupplier<java.io.InputStreamReader> newReaderSupplier(java.io.File file,
java.nio.charset.Charset charset)
InputStreamReader that read a file using the given character set.
file - the file to read fromcharset - the character set used when reading the file
public static OutputSupplier<java.io.OutputStreamWriter> newWriterSupplier(java.io.File file,
java.nio.charset.Charset charset)
OutputStreamWriter
that write to a file using the given character set.
file - the file to write tocharset - the character set used when writing the file
public static OutputSupplier<java.io.OutputStreamWriter> newWriterSupplier(java.io.File file,
java.nio.charset.Charset charset,
boolean append)
OutputStreamWriter
that write to or append to a file using the given character set.
file - the file to write tocharset - the character set used when writing the fileappend - if true, the encoded characters will be appended to the file;
otherwise the file is overwritten
public static byte[] toByteArray(java.io.File file)
throws java.io.IOException
file - the file to read from
java.lang.IllegalArgumentException - if the file is bigger than the largest
possible byte array (2^31 - 1)
java.io.IOException - if an I/O error occurs
public 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 character set used when reading the file
java.io.IOException - if an I/O error occurs
public static void copy(InputSupplier<? extends java.io.InputStream> from,
java.io.File to)
throws java.io.IOException
InputStream supplied by a
factory.
from - the input factoryto - the destination file
java.io.IOException - if an I/O error occurs
public static void write(byte[] from,
java.io.File to)
throws java.io.IOException
from - the bytes to writeto - the destination file
java.io.IOException - if an I/O error occurs
public static void copy(java.io.File from,
OutputSupplier<? extends java.io.OutputStream> to)
throws java.io.IOException
OutputStream supplied by
a factory.
from - the source fileto - the output factory
java.io.IOException - if an I/O error occurs
public static void copy(java.io.File from,
java.io.OutputStream to)
throws java.io.IOException
from - the source fileto - the output stream
java.io.IOException - if an I/O error occurs
public static void copy(java.io.File from,
java.io.File to)
throws java.io.IOException
from - the source fileto - the destination file
java.io.IOException - if an I/O error occurs
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
Readable and
Closeable object supplied by a factory, using the given
character set.
from - the readable supplierto - the destination filecharset - the character set used when writing the file
java.io.IOException - if an I/O error occurs
public 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 character set used when writing the file
java.io.IOException - if an I/O error occurs
public 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 character set used when writing the file
java.io.IOException - if an I/O error occurs
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
Appendable &
Closeable object supplied by a factory, using the given
character set.
from - the source filecharset - the character set used when reading the fileto - the appendable supplier
java.io.IOException - if an I/O error occurs
public 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 character set used when reading the fileto - the appendable object
java.io.IOException - if an I/O error occurs
public 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 created
public static void touch(java.io.File file)
throws java.io.IOException
file - the file to create or update
java.io.IOException - if an I/O error occurs
public static void move(java.io.File from,
java.io.File to)
throws java.io.IOException
mv command.
from - the source fileto - the destination file
java.io.IOException - if an I/O error occurs
public static void deleteDirectoryContents(java.io.File directory)
throws java.io.IOException
If the file argument is a symbolic link or there is a symbolic link in the path leading to the directory, this method will do nothing. Symbolic links within the directory are not followed.
directory - the directory to delete the contents of
java.lang.IllegalArgumentException - if the argument is not a directory
java.io.IOException - if an I/O error occursdeleteRecursively(java.io.File)
public static void deleteRecursively(java.io.File file)
throws java.io.IOException
If the file argument is a symbolic link the link will be deleted but not the target of the link. If the argument is a directory, symbolic links within the directory will not be followed.
file - the file to delete
java.io.IOException - if an I/O error occursdeleteDirectoryContents(java.io.File)
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 character set used when writing the file
java.io.IOException - if an I/O error occurs
public static java.util.List<java.lang.String> readLines(java.io.File file,
java.nio.charset.Charset charset)
throws java.io.IOException
file - the file to read fromcharset - the character set used when writing the file
List containing all the lines
java.io.IOException - if an I/O error occurs
public 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 character set used when writing the filecallback - the LineProcessor to use to handle the lines
java.io.IOException - if an I/O error occurs
public 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 occurs
public static long getChecksum(java.io.File file,
java.util.zip.Checksum checksum)
throws java.io.IOException
file - the file to readchecksum - the checksum object
Checksum.getValue() after updating the
checksum object with all of the bytes in the file
java.io.IOException - if an I/O error occurs
public static byte[] getDigest(java.io.File file,
java.security.MessageDigest md)
throws java.io.IOException
file - the file to readmd - the digest object
MessageDigest.digest() after updating the
digest object with all of the bytes in this file
java.io.IOException - if an I/O error occurs
public 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 map
file
java.io.FileNotFoundException - if the file does not exist
java.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 file
file
java.io.FileNotFoundException - if the file does not exist
java.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 file
file
java.io.IOException - if an I/O error occurs
java.io.FileNotFoundExceptionFileChannel.map(MapMode, long, long)
|
|||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | ||||||||