|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Object com.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(CharSequence from,
File to,
Charset charset)
Appends a character sequence (such as a string) to a file using the given character set. |
|
static void |
copy(File from,
Charset charset,
Appendable to)
Copies all characters from a file to an appendable object, using the given character set. |
|
static
|
copy(File from,
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(File from,
File to)
Copies all the bytes from one file to another. |
|
static void |
copy(File from,
OutputStream to)
Copies all bytes from a file to an output stream. |
|
static void |
copy(File from,
OutputSupplier<? extends OutputStream> to)
Copies all bytes from a file to an OutputStream supplied by
a factory. |
|
static void |
copy(InputSupplier<? extends InputStream> from,
File to)
Copies to a file all bytes from an InputStream supplied by a
factory. |
|
static
|
copy(InputSupplier<R> from,
File to,
Charset charset)
Copies to a file all characters from a Readable and
Closeable object supplied by a factory, using the given
character set. |
|
static void |
createParentDirs(File file)
Creates any necessary but nonexistent parent directories of the specified file. |
|
static 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(File file1,
File file2)
Returns true if the files contains the same bytes. |
|
static long |
getChecksum(File file,
Checksum checksum)
Computes and returns the checksum value for a file. |
|
static byte[] |
getDigest(File file,
MessageDigest md)
Deprecated. Use hash(java.io.File, com.google.common.hash.HashFunction) instead. For example,
Files.hash(file, Hashing.sha1()) . This method is scheduled to
be removed in Guava release 13.0. |
|
static String |
getFileExtension(String fileName)
Returns the file extension for the given file name, or the empty string if the file has no extension. |
|
static HashCode |
hash(File file,
HashFunction hashFunction)
Computes the hash code of the file using hashFunction . |
|
static MappedByteBuffer |
map(File file)
Fully maps a file read-only in to memory as per FileChannel.map(java.nio.channels.FileChannel.MapMode, long, long) . |
|
static MappedByteBuffer |
map(File file,
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 MappedByteBuffer |
map(File file,
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(File from,
File to)
Moves the file from one path to another. |
|
static InputSupplier<FileInputStream> |
newInputStreamSupplier(File file)
Returns a factory that will supply instances of FileInputStream
that read from a file. |
|
static OutputSupplier<FileOutputStream> |
newOutputStreamSupplier(File file)
Returns a factory that will supply instances of FileOutputStream
that write to a file. |
|
static OutputSupplier<FileOutputStream> |
newOutputStreamSupplier(File file,
boolean append)
Returns a factory that will supply instances of FileOutputStream
that write to or append to a file. |
|
static BufferedReader |
newReader(File file,
Charset charset)
Returns a buffered reader that reads from a file using the given character set. |
|
static InputSupplier<InputStreamReader> |
newReaderSupplier(File file,
Charset charset)
Returns a factory that will supply instances of InputStreamReader that read a file using the given character set. |
|
static BufferedWriter |
newWriter(File file,
Charset charset)
Returns a buffered writer that writes to a file using the given character set. |
|
static OutputSupplier<OutputStreamWriter> |
newWriterSupplier(File file,
Charset charset)
Returns a factory that will supply instances of OutputStreamWriter
that write to a file using the given character set. |
|
static OutputSupplier<OutputStreamWriter> |
newWriterSupplier(File file,
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(File file,
ByteProcessor<T> processor)
Process the bytes of a file. |
|
static String |
readFirstLine(File file,
Charset charset)
Reads the first line from a file. |
|
static List<String> |
readLines(File file,
Charset charset)
Reads all of the lines from a file. |
|
static
|
readLines(File file,
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 String |
simplifyPath(String pathname)
Returns the lexically cleaned form of the path name, usually (but not always) equivalent to the original. |
|
static byte[] |
toByteArray(File file)
Reads all bytes from a file into a byte array. |
|
static String |
toString(File file,
Charset charset)
Reads all characters from a file into a String , using the given
character set. |
|
static void |
touch(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,
File to)
Overwrites a file with the contents of a byte array. |
|
static void |
write(CharSequence from,
File to,
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 BufferedReader newReader(File file, Charset charset) throws FileNotFoundException
file
- the file to read fromcharset
- the charset used to decode the input stream; see Charsets
for helpful predefined constants
FileNotFoundException
public static BufferedWriter newWriter(File file, Charset charset) throws FileNotFoundException
file
- the file to write tocharset
- the charset used to encode the output stream; see Charsets
for helpful predefined constants
FileNotFoundException
public static InputSupplier<FileInputStream> newInputStreamSupplier(File file)
FileInputStream
that read from a file.
file
- the file to read from
public static OutputSupplier<FileOutputStream> newOutputStreamSupplier(File file)
FileOutputStream
that write to a file.
file
- the file to write to
public static OutputSupplier<FileOutputStream> newOutputStreamSupplier(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<InputStreamReader> newReaderSupplier(File file, Charset charset)
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
public static OutputSupplier<OutputStreamWriter> newWriterSupplier(File file, Charset charset)
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
public static OutputSupplier<OutputStreamWriter> newWriterSupplier(File file, 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 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 overwritten
public static byte[] toByteArray(File file) throws IOException
file
- the file to read from
IllegalArgumentException
- if the file is bigger than the largest
possible byte array (2^31 - 1)
IOException
- if an I/O error occurspublic static String toString(File file, Charset charset) throws 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 constants
IOException
- if an I/O error occurspublic static void copy(InputSupplier<? extends InputStream> from, File to) throws IOException
InputStream
supplied by a
factory.
from
- the input factoryto
- the destination file
IOException
- if an I/O error occurspublic static void write(byte[] from, File to) throws IOException
from
- the bytes to writeto
- the destination file
IOException
- if an I/O error occurspublic static void copy(File from, OutputSupplier<? extends OutputStream> to) throws IOException
OutputStream
supplied by
a factory.
from
- the source fileto
- the output factory
IOException
- if an I/O error occurspublic static void copy(File from, OutputStream to) throws IOException
from
- the source fileto
- the output stream
IOException
- if an I/O error occurspublic static void copy(File from, File to) throws IOException
from
- the source fileto
- the destination file
IOException
- if an I/O error occurs
IllegalArgumentException
- if from.equals(to)
public static <R extends Readable & Closeable> void copy(InputSupplier<R> from, File to, Charset charset) throws IOException
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 constants
IOException
- if an I/O error occurspublic static void write(CharSequence from, File to, Charset charset) throws IOException
from
- the character sequence to writeto
- the destination filecharset
- the charset used to encode the output stream; see Charsets
for helpful predefined constants
IOException
- if an I/O error occurspublic static void append(CharSequence from, File to, Charset charset) throws IOException
from
- the character sequence to appendto
- the destination filecharset
- the charset used to encode the output stream; see Charsets
for helpful predefined constants
IOException
- if an I/O error occurspublic static <W extends Appendable & Closeable> void copy(File from, Charset charset, OutputSupplier<W> to) throws IOException
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 supplier
IOException
- if an I/O error occurspublic static void copy(File from, Charset charset, Appendable to) throws IOException
from
- the source filecharset
- the charset used to decode the input stream; see Charsets
for helpful predefined constantsto
- the appendable object
IOException
- if an I/O error occurspublic static boolean equal(File file1, File file2) throws IOException
IOException
- if an I/O error occurspublic static 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.
IllegalStateException
- if the directory could not be createdpublic static void touch(File file) throws IOException
file
- the file to create or update
IOException
- if an I/O error occurspublic static void createParentDirs(File file) throws IOException
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(File from, File to) throws IOException
mv
command.
from
- the source fileto
- the destination file
IOException
- if an I/O error occurs
IllegalArgumentException
- if from.equals(to)
public static String readFirstLine(File file, Charset charset) throws IOException
file
- the file to read fromcharset
- the charset used to decode the input stream; see Charsets
for helpful predefined constants
IOException
- if an I/O error occurspublic static List<String> readLines(File file, Charset charset) throws IOException
file
- the file to read fromcharset
- the charset used to decode the input stream; see Charsets
for helpful predefined constants
List
containing all the lines
IOException
- if an I/O error occurspublic static <T> T readLines(File file, Charset charset, LineProcessor<T> callback) throws 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 lines
IOException
- if an I/O error occurspublic static <T> T readBytes(File file, ByteProcessor<T> processor) throws 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.
IOException
- if an I/O error occurspublic static long getChecksum(File file, Checksum checksum) throws IOException
file
- the file to readchecksum
- the checksum object
Checksum.getValue()
after updating the
checksum object with all of the bytes in the file
IOException
- if an I/O error occurs@Deprecated public static byte[] getDigest(File file, MessageDigest md) throws IOException
hash(java.io.File, com.google.common.hash.HashFunction)
instead. For example,
Files.hash(file, Hashing.sha1())
. This method is scheduled to
be removed in Guava release 13.0.
file
- the file to readmd
- the digest object
MessageDigest.digest()
after updating the
digest object with all of the bytes in this file
IOException
- if an I/O error occurspublic static HashCode hash(File file, HashFunction hashFunction) throws IOException
file
using hashFunction
.
file
- the file to readhashFunction
- the hash function to use to hash the data
HashCode
of all of the bytes in the file
IOException
- if an I/O error occurspublic static MappedByteBuffer map(File file) throws 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
FileNotFoundException
- if the file
does not exist
IOException
- if an I/O error occursFileChannel.map(MapMode, long, long)
public static MappedByteBuffer map(File file, FileChannel.MapMode mode) throws 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
FileNotFoundException
- if the file
does not exist
IOException
- if an I/O error occursFileChannel.map(MapMode, long, long)
public static MappedByteBuffer map(File file, FileChannel.MapMode mode, long size) throws FileNotFoundException, 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
IOException
- if an I/O error occurs
FileNotFoundException
FileChannel.map(MapMode, long, long)
public static String simplifyPath(String pathname)
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 String getFileExtension(String fileName)
.
'.
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |