Modifier and Type | Method and Description |
---|---|
static void |
append(CharSequence from,
File to,
Charset charset)
Deprecated.
Prefer
asCharSink(to, charset, FileWriteMode.APPEND).write(from) . This
method is scheduled to be removed in January 2019. |
static ByteSink |
asByteSink(File file,
FileWriteMode... modes)
Returns a new
ByteSink for writing bytes to the given file. |
static ByteSource |
asByteSource(File file)
Returns a new
ByteSource for reading bytes from the given file. |
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. |
static CharSource |
asCharSource(File file,
Charset charset)
Returns a new
CharSource for reading character data from the given file using the given
character set. |
static void |
copy(File from,
Charset charset,
Appendable to)
Deprecated.
Prefer
asCharSource(from, charset).copyTo(to) . This method is scheduled to
be removed in January 2019. |
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 |
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 given files exist, are not directories, and contain the same bytes.
|
static Traverser<File> |
fileTraverser()
Returns a
Traverser instance for the file and directory tree. |
static TreeTraverser<File> |
fileTreeTraverser()
Deprecated.
The returned
TreeTraverser type is deprecated. Use the replacement method
fileTraverser() instead with the same semantics as this method. This method is
scheduled to be removed in April 2018. |
static String |
getFileExtension(String fullName)
Returns the file extension for
the given file name, or the empty string if the file has no extension.
|
static String |
getNameWithoutExtension(String file)
Returns the file name without its file extension or path.
|
static HashCode |
hash(File file,
HashFunction hashFunction)
Deprecated.
Prefer
asByteSource(file).hash(hashFunction) . This method is scheduled to
be removed in January 2019. |
static Predicate<File> |
isDirectory()
Returns a predicate that returns the result of
File.isDirectory() on input files. |
static Predicate<File> |
isFile()
Returns a predicate that returns the result of
File.isFile() on input files. |
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 a file from one path to another.
|
static BufferedReader |
newReader(File file,
Charset charset)
Returns a buffered reader that reads from 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 <T> T |
readBytes(File file,
ByteProcessor<T> processor)
Deprecated.
Prefer
asByteSource(file).read(processor) . This method is scheduled to be
removed in January 2019. |
static String |
readFirstLine(File file,
Charset charset)
Deprecated.
Prefer
asCharSource(file, charset).readFirstLine() . This method is
scheduled to be removed in January 2019. |
static List<String> |
readLines(File file,
Charset charset)
Reads all of the lines from a file.
|
static <T> T |
readLines(File file,
Charset charset,
LineProcessor<T> callback)
Deprecated.
Prefer
asCharSource(file, charset).readLines(callback) . This method is
scheduled to be removed in January 2019. |
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)
Deprecated.
Prefer
asCharSource(file, charset).read() . This method is scheduled to be
removed in January 2019. |
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)
Deprecated.
Prefer
asCharSink(to, charset).write(from) . This method is scheduled to be
removed in January 2019. |
public static BufferedReader newReader(File file, Charset charset) throws FileNotFoundException
Path
equivalent: Files.newBufferedReader(java.nio.file.Path, Charset)
.
file
- the file to read fromcharset
- the charset used to decode the input stream; see StandardCharsets
for
helpful predefined constantsFileNotFoundException
public static BufferedWriter newWriter(File file, Charset charset) throws FileNotFoundException
Path
equivalent: Files.newBufferedWriter(java.nio.file.Path, Charset,
java.nio.file.OpenOption...)
.
file
- the file to write tocharset
- the charset used to encode the output stream; see StandardCharsets
for
helpful predefined constantsFileNotFoundException
public static ByteSource asByteSource(File file)
ByteSource
for reading bytes from the given file.public static ByteSink asByteSink(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(File file, Charset charset)
CharSource
for reading character data from the given file using the given
character set.public static CharSink asCharSink(File file, 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.public static byte[] toByteArray(File file) throws IOException
Path
equivalent: Files.readAllBytes(java.nio.file.Path)
.
file
- the file to read fromIllegalArgumentException
- if the file is bigger than the largest possible byte array
(2^31 - 1)IOException
- if an I/O error occurs@Deprecated public static String toString(File file, Charset charset) throws IOException
asCharSource(file, charset).read()
. This method is scheduled to be
removed in January 2019.String
, using the given character set.file
- the file to read fromcharset
- the charset used to decode the input stream; see StandardCharsets
for
helpful predefined constantsIOException
- if an I/O error occurspublic static void write(byte[] from, File to) throws IOException
Path
equivalent: Files.write(java.nio.file.Path, byte[], java.nio.file.OpenOption...)
.
from
- the bytes to writeto
- the destination fileIOException
- if an I/O error occurs@Deprecated public static void write(CharSequence from, File to, Charset charset) throws IOException
asCharSink(to, charset).write(from)
. This method is scheduled to be
removed in January 2019.from
- the character sequence to writeto
- the destination filecharset
- the charset used to encode the output stream; see StandardCharsets
for
helpful predefined constantsIOException
- if an I/O error occurspublic static void copy(File from, OutputStream to) throws IOException
Path
equivalent: Files.copy(java.nio.file.Path, OutputStream)
.
from
- the source fileto
- the output streamIOException
- if an I/O error occurspublic static void copy(File from, File to) throws IOException
Copying is not an atomic operation - in the case of an I/O error, power loss, process
termination, or other problems, to
may not be a complete copy of from
. If you
need to guard against those conditions, you should employ other file-level synchronization.
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.
Path
equivalent: Files.copy(java.nio.file.Path, java.nio.file.Path, java.nio.file.CopyOption...)
.
from
- the source fileto
- the destination fileIOException
- if an I/O error occursIllegalArgumentException
- if from.equals(to)
@Deprecated public static void copy(File from, Charset charset, Appendable to) throws IOException
asCharSource(from, charset).copyTo(to)
. This method is scheduled to
be removed in January 2019.from
- the source filecharset
- the charset used to decode the input stream; see StandardCharsets
for
helpful predefined constantsto
- the appendable objectIOException
- if an I/O error occurs@Deprecated public static void append(CharSequence from, File to, Charset charset) throws IOException
asCharSink(to, charset, FileWriteMode.APPEND).write(from)
. This
method is scheduled to be removed in January 2019.from
- the character sequence to appendto
- the destination filecharset
- the charset used to encode the output stream; see StandardCharsets
for
helpful predefined constantsIOException
- 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.
Path
equivalent: Files.createTempDirectory(java.nio.file.Path, java.lang.String, java.nio.file.attribute.FileAttribute<?>...)
.
IllegalStateException
- if the directory could not be createdpublic static void touch(File file) throws IOException
file
- the file to create or updateIOException
- 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
to
must be the target path for the file itself; not
just the new name for the file or the path to the new parent directory.
Path
equivalent: Files.move(java.nio.file.Path, java.nio.file.Path, java.nio.file.CopyOption...)
.
from
- the source fileto
- the destination fileIOException
- if an I/O error occursIllegalArgumentException
- if from.equals(to)
@Deprecated public static String readFirstLine(File file, Charset charset) throws IOException
asCharSource(file, charset).readFirstLine()
. This method is
scheduled to be removed in January 2019.file
- the file to read fromcharset
- the charset used to decode the input stream; see StandardCharsets
for
helpful predefined constantsIOException
- if an I/O error occurspublic static List<String> readLines(File file, Charset charset) throws IOException
This method returns a mutable List
. For an ImmutableList
, use Files.asCharSource(file, charset).readLines()
.
Path
equivalent: Files.readAllLines(java.nio.file.Path, Charset)
.
file
- the file to read fromcharset
- the charset used to decode the input stream; see StandardCharsets
for
helpful predefined constantsList
containing all the linesIOException
- if an I/O error occurs@Deprecated @CanIgnoreReturnValue public static <T> T readLines(File file, Charset charset, LineProcessor<T> callback) throws IOException
asCharSource(file, charset).readLines(callback)
. This method is
scheduled to be removed in January 2019.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 StandardCharsets
for
helpful predefined constantscallback
- the LineProcessor
to use to handle the linesIOException
- if an I/O error occurs@Deprecated @CanIgnoreReturnValue public static <T> T readBytes(File file, ByteProcessor<T> processor) throws IOException
asByteSource(file).read(processor)
. This method is scheduled to be
removed in January 2019.(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 occurs@Deprecated public static HashCode hash(File file, HashFunction hashFunction) throws IOException
asByteSource(file).hash(hashFunction)
. This method is scheduled to
be removed in January 2019.file
using hashFunction
.file
- the file to readhashFunction
- the hash function to use to hash the dataHashCode
of all of the bytes in the fileIOException
- 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 mapfile
FileNotFoundException
- if the file
does not existIOException
- 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 existIOException
- 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 occursFileNotFoundException
FileChannel.map(MapMode, long, long)
public static String simplifyPath(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 String getFileExtension(String fullName)
.
'.
Note: This method simply returns everything after the last '.
' in the file's
name as determined by File.getName()
. It does not account for any filesystem-specific
behavior that the File
API does not already account for. For example, on NTFS it will
report "txt"
as the extension for the filename "foo.exe:.txt"
even though NTFS
will drop the ":.txt"
part of the name when the file is actually created on the
filesystem due to NTFS's Alternate Data Streams.
public static String getNameWithoutExtension(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.@Deprecated public static TreeTraverser<File> fileTreeTraverser()
TreeTraverser
type is deprecated. Use the replacement method
fileTraverser()
instead with the same semantics as this method. This method is
scheduled to be removed in April 2018.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 Traverser<File> fileTraverser()
Traverser
instance for the file and directory tree. The returned traverser
starts from a File
and will return all files and directories it encounters.
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.
If available, consider using MoreFiles.fileTraverser()
instead. It behaves the same
except that it doesn't follow symbolic links and returns Path
instances.
If the File
passed to one of the Traverser
methods does not exist or is not
a directory, no exception will be thrown and the returned Iterable
will contain a
single element: that file.
Example: Files.fileTraverser().breadthFirst("/")
may return files with the following
paths: ["/", "/etc", "/home", "/usr", "/etc/config.txt", "/etc/fonts", ...]
public static Predicate<File> isDirectory()
File.isDirectory()
on input files.public static Predicate<File> isFile()
File.isFile()
on input files.Copyright © 2010–2018. All rights reserved.