Class Files
All method parameters must be non-null unless documented otherwise.
- Since:
- 1.0
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
append
(CharSequence from, File to, Charset charset) Deprecated.Appends a character sequence (such as a string) to a file using the given character set.static ByteSink
asByteSink
(File file, FileWriteMode... modes) Deprecated.Returns a newByteSink
for writing bytes to the given file.static ByteSource
asByteSource
(File file) Deprecated.Returns a newByteSource
for reading bytes from the given file.static CharSink
asCharSink
(File file, Charset charset, FileWriteMode... modes) Deprecated.Returns a newCharSink
for writing character data to the given file using the given character set.static CharSource
asCharSource
(File file, Charset charset) Deprecated.Returns a newCharSource
for reading character data from the given file using the given character set.static void
copy
(InputSupplier<? extends InputStream> from, File to) Deprecated.Copies to a file all bytes from anInputStream
supplied by a factory.copy
(InputSupplier<R> from, File to, Charset charset) Deprecated.static void
copy
(File from, OutputSupplier<? extends OutputStream> to) Deprecated.Copies all bytes from a file to anOutputStream
supplied by a factory.static void
Deprecated.Copies all the bytes from one file to another.static void
copy
(File from, OutputStream to) Deprecated.Copies all bytes from a file to an output stream.static <W extends Appendable & Closeable>
voidcopy
(File from, Charset charset, OutputSupplier<W> to) Deprecated.Copies all characters from a file to aAppendable
invalid input: '&'Closeable
object supplied by a factory, using the given character set.static void
copy
(File from, Charset charset, Appendable to) Deprecated.Copies all characters from a file to an appendable object, using the given character set.static void
createParentDirs
(File file) Deprecated.Creates any necessary but nonexistent parent directories of the specified file.static File
Deprecated.Atomically creates a new directory somewhere beneath the system's temporary directory (as defined by thejava.io.tmpdir
system property), and returns its name.static boolean
Deprecated.Returns true if the files contains the same bytes.static TreeTraverser
<File> Deprecated.Returns aTreeTraverser
instance forFile
trees.static String
getFileExtension
(String fullName) Deprecated.Returns the file extension for the given file name, or the empty string if the file has no extension.static String
Deprecated.Returns the file name without its file extension or path.static HashCode
hash
(File file, HashFunction hashFunction) Deprecated.Computes the hash code of thefile
usinghashFunction
.Deprecated.Returns a predicate that returns the result ofFile.isDirectory()
on input files.isFile()
Deprecated.Returns a predicate that returns the result ofFile.isFile()
on input files.static MappedByteBuffer
Deprecated.Fully maps a file read-only in to memory as perFileChannel.map(java.nio.channels.FileChannel.MapMode, long, long)
.static MappedByteBuffer
map
(File file, FileChannel.MapMode mode) Deprecated.Fully maps a file in to memory as perFileChannel.map(java.nio.channels.FileChannel.MapMode, long, long)
using the requestedFileChannel.MapMode
.static MappedByteBuffer
map
(File file, FileChannel.MapMode mode, long size) Deprecated.Maps a file in to memory as perFileChannel.map(java.nio.channels.FileChannel.MapMode, long, long)
using the requestedFileChannel.MapMode
.static void
Deprecated.Moves the file from one path to another.static InputSupplier
<FileInputStream> newInputStreamSupplier
(File file) Deprecated.Returns a factory that will supply instances ofFileInputStream
that read from a file.static OutputSupplier
<FileOutputStream> newOutputStreamSupplier
(File file) Deprecated.Returns a factory that will supply instances ofFileOutputStream
that write to a file.static OutputSupplier
<FileOutputStream> newOutputStreamSupplier
(File file, boolean append) Deprecated.Returns a factory that will supply instances ofFileOutputStream
that write to or append to a file.static BufferedReader
Deprecated.Returns a buffered reader that reads from a file using the given character set.static InputSupplier
<InputStreamReader> newReaderSupplier
(File file, Charset charset) Deprecated.Returns a factory that will supply instances ofInputStreamReader
that read a file using the given character set.static BufferedWriter
Deprecated.Returns a buffered writer that writes to a file using the given character set.static OutputSupplier
<OutputStreamWriter> newWriterSupplier
(File file, Charset charset) Deprecated.Returns a factory that will supply instances ofOutputStreamWriter
that write to a file using the given character set.static OutputSupplier
<OutputStreamWriter> newWriterSupplier
(File file, Charset charset, boolean append) Deprecated.Returns a factory that will supply instances ofOutputStreamWriter
that write to or append to a file using the given character set.static <T> T
readBytes
(File file, ByteProcessor<T> processor) Deprecated.Process the bytes of a file.static String
readFirstLine
(File file, Charset charset) Deprecated.Reads the first line from a file.Deprecated.Reads all of the lines from a file.static <T> T
readLines
(File file, Charset charset, LineProcessor<T> callback) Deprecated.Streams lines from aFile
, stopping when our callback returns false, or we have read all of the lines.static String
simplifyPath
(String pathname) Deprecated.Returns the lexically cleaned form of the path name, usually (but not always) equivalent to the original.static byte[]
toByteArray
(File file) Deprecated.Reads all bytes from a file into a byte array.static String
Deprecated.Reads all characters from a file into aString
, using the given character set.static void
Deprecated.Creates an empty file or updates the last updated timestamp on the same as the unix command of the same name.static void
Deprecated.Overwrites a file with the contents of a byte array.static void
write
(CharSequence from, File to, Charset charset) Deprecated.Writes a character sequence (such as a string) to a file using the given character set.
-
Method Details
-
newReader
Deprecated.Returns a buffered reader that reads from a file using the given character set.- Parameters:
file
- the file to read fromcharset
- the charset used to decode the input stream; seeCharsets
for helpful predefined constants- Returns:
- the buffered reader
- Throws:
FileNotFoundException
-
newWriter
Deprecated.Returns a buffered writer that writes to a file using the given character set.- Parameters:
file
- the file to write tocharset
- the charset used to encode the output stream; seeCharsets
for helpful predefined constants- Returns:
- the buffered writer
- Throws:
FileNotFoundException
-
asByteSource
Deprecated.Returns a newByteSource
for reading bytes from the given file.- Since:
- 14.0
-
asByteSink
Deprecated.Returns a newByteSink
for writing bytes to the given file. The givenmodes
control how the file is opened for writing. When no mode is provided, the file will be truncated before writing. When theAPPEND
mode is provided, writes will append to the end of the file without truncating it.- Since:
- 14.0
-
asCharSource
Deprecated.Returns a newCharSource
for reading character data from the given file using the given character set.- Since:
- 14.0
-
asCharSink
Deprecated.Returns a newCharSink
for writing character data to the given file using the given character set. The givenmodes
control how the file is opened for writing. When no mode is provided, the file will be truncated before writing. When theAPPEND
mode is provided, writes will append to the end of the file without truncating it.- Since:
- 14.0
-
newInputStreamSupplier
Deprecated.Returns a factory that will supply instances ofFileInputStream
that read from a file.- Parameters:
file
- the file to read from- Returns:
- the factory
-
newOutputStreamSupplier
Deprecated.Returns a factory that will supply instances ofFileOutputStream
that write to a file.- Parameters:
file
- the file to write to- Returns:
- the factory
-
newOutputStreamSupplier
Deprecated.Returns a factory that will supply instances ofFileOutputStream
that write to or append to a file.- Parameters:
file
- the file to write toappend
- if true, the encoded characters will be appended to the file; otherwise the file is overwritten- Returns:
- the factory
-
newReaderSupplier
Deprecated.Returns a factory that will supply instances ofInputStreamReader
that read a file using the given character set.- Parameters:
file
- the file to read fromcharset
- the charset used to decode the input stream; seeCharsets
for helpful predefined constants- Returns:
- the factory
-
newWriterSupplier
Deprecated.Returns a factory that will supply instances ofOutputStreamWriter
that write to a file using the given character set.- Parameters:
file
- the file to write tocharset
- the charset used to encode the output stream; seeCharsets
for helpful predefined constants- Returns:
- the factory
-
newWriterSupplier
public static OutputSupplier<OutputStreamWriter> newWriterSupplier(File file, Charset charset, boolean append) Deprecated.Returns a factory that will supply instances ofOutputStreamWriter
that write to or append to a file using the given character set.- Parameters:
file
- the file to write tocharset
- the charset used to encode the output stream; seeCharsets
for helpful predefined constantsappend
- if true, the encoded characters will be appended to the file; otherwise the file is overwritten- Returns:
- the factory
-
toByteArray
Deprecated.Reads all bytes from a file into a byte array.- Parameters:
file
- the file to read from- Returns:
- a byte array containing all the bytes from file
- Throws:
IllegalArgumentException
- if the file is bigger than the largest possible byte array (2^31 - 1)IOException
- if an I/O error occurs
-
toString
Deprecated.Reads all characters from a file into aString
, using the given character set.- Parameters:
file
- the file to read fromcharset
- the charset used to decode the input stream; seeCharsets
for helpful predefined constants- Returns:
- a string containing all the characters from the file
- Throws:
IOException
- if an I/O error occurs
-
copy
Deprecated.Copies to a file all bytes from anInputStream
supplied by a factory.- Parameters:
from
- the input factoryto
- the destination file- Throws:
IOException
- if an I/O error occurs
-
write
Deprecated.Overwrites a file with the contents of a byte array.- Parameters:
from
- the bytes to writeto
- the destination file- Throws:
IOException
- if an I/O error occurs
-
copy
Deprecated.Copies all bytes from a file to anOutputStream
supplied by a factory.- Parameters:
from
- the source fileto
- the output factory- Throws:
IOException
- if an I/O error occurs
-
copy
Deprecated.Copies all bytes from a file to an output stream.- Parameters:
from
- the source fileto
- the output stream- Throws:
IOException
- if an I/O error occurs
-
copy
Deprecated.Copies all the bytes from one file to another.Warning: If
to
represents an existing file, that file will be overwritten with the contents offrom
. Ifto
andfrom
refer to the same file, the contents of that file will be deleted.- Parameters:
from
- the source fileto
- the destination file- Throws:
IOException
- if an I/O error occursIllegalArgumentException
- iffrom.equals(to)
-
copy
public static <R extends Readable & Closeable> void copy(InputSupplier<R> from, File to, Charset charset) throws IOException Deprecated.Copies to a file all characters from aReadable
andCloseable
object supplied by a factory, using the given character set.- Parameters:
from
- the readable supplierto
- the destination filecharset
- the charset used to encode the output stream; seeCharsets
for helpful predefined constants- Throws:
IOException
- if an I/O error occurs
-
write
Deprecated.Writes a character sequence (such as a string) to a file using the given character set.- Parameters:
from
- the character sequence to writeto
- the destination filecharset
- the charset used to encode the output stream; seeCharsets
for helpful predefined constants- Throws:
IOException
- if an I/O error occurs
-
append
Deprecated.Appends a character sequence (such as a string) to a file using the given character set.- Parameters:
from
- the character sequence to appendto
- the destination filecharset
- the charset used to encode the output stream; seeCharsets
for helpful predefined constants- Throws:
IOException
- if an I/O error occurs
-
copy
public static <W extends Appendable & Closeable> void copy(File from, Charset charset, OutputSupplier<W> to) throws IOException Deprecated.Copies all characters from a file to aAppendable
invalid input: '&'Closeable
object supplied by a factory, using the given character set.- Parameters:
from
- the source filecharset
- the charset used to decode the input stream; seeCharsets
for helpful predefined constantsto
- the appendable supplier- Throws:
IOException
- if an I/O error occurs
-
copy
Deprecated.Copies all characters from a file to an appendable object, using the given character set.- Parameters:
from
- the source filecharset
- the charset used to decode the input stream; seeCharsets
for helpful predefined constantsto
- the appendable object- Throws:
IOException
- if an I/O error occurs
-
equal
Deprecated.Returns true if the files contains the same bytes.- Throws:
IOException
- if an I/O error occurs
-
createTempDir
Deprecated.Atomically creates a new directory somewhere beneath the system's temporary directory (as defined by thejava.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 callcreateTempFile
, 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.
- Returns:
- the newly-created directory
- Throws:
IllegalStateException
- if the directory could not be created
-
touch
Deprecated.Creates an empty file or updates the last updated timestamp on the same as the unix command of the same name.- Parameters:
file
- the file to create or update- Throws:
IOException
- if an I/O error occurs
-
createParentDirs
Deprecated.Creates any necessary but nonexistent parent directories of the specified file. Note that if this operation fails it may have succeeded in creating some (but not all) of the necessary parent directories.- Throws:
IOException
- if an I/O error occurs, or if any necessary but nonexistent parent directories of the specified file could not be created.- Since:
- 4.0
-
move
Deprecated.Moves the file from one path to another. This method can rename a file or move it to a different directory, like the Unixmv
command.- Parameters:
from
- the source fileto
- the destination file- Throws:
IOException
- if an I/O error occursIllegalArgumentException
- iffrom.equals(to)
-
readFirstLine
Deprecated.Reads the first line from a file. The line does not include line-termination characters, but does include other leading and trailing whitespace.- Parameters:
file
- the file to read fromcharset
- the charset used to decode the input stream; seeCharsets
for helpful predefined constants- Returns:
- the first line, or null if the file is empty
- Throws:
IOException
- if an I/O error occurs
-
readLines
Deprecated.Reads all of the lines from a file. The lines do not include line-termination characters, but do include other leading and trailing whitespace.This method returns a mutable
List
. For anImmutableList
, useFiles.asCharSource(file, charset).readLines()
.- Parameters:
file
- the file to read fromcharset
- the charset used to decode the input stream; seeCharsets
for helpful predefined constants- Returns:
- a mutable
List
containing all the lines - Throws:
IOException
- if an I/O error occurs
-
readLines
public static <T> T readLines(File file, Charset charset, LineProcessor<T> callback) throws IOException Deprecated.Streams lines from aFile
, stopping when our callback returns false, or we have read all of the lines.- Parameters:
file
- the file to read fromcharset
- the charset used to decode the input stream; seeCharsets
for helpful predefined constantscallback
- theLineProcessor
to use to handle the lines- Returns:
- the output of processing the lines
- Throws:
IOException
- if an I/O error occurs
-
readBytes
Deprecated.Process the bytes of a file.(If this seems too complicated, maybe you're looking for
toByteArray(java.io.File)
.)- Parameters:
file
- the file to readprocessor
- the object to which the bytes of the file are passed.- Returns:
- the result of the byte processor
- Throws:
IOException
- if an I/O error occurs
-
hash
Deprecated.Computes the hash code of thefile
usinghashFunction
.- Parameters:
file
- the file to readhashFunction
- the hash function to use to hash the data- Returns:
- the
HashCode
of all of the bytes in the file - Throws:
IOException
- if an I/O error occurs- Since:
- 12.0
-
map
Deprecated.Fully maps a file read-only in to memory as perFileChannel.map(java.nio.channels.FileChannel.MapMode, long, long)
.Files are mapped from offset 0 to its length.
This only works for files invalid input: '<'=
Integer.MAX_VALUE
bytes.- Parameters:
file
- the file to map- Returns:
- a read-only buffer reflecting
file
- Throws:
FileNotFoundException
- if thefile
does not existIOException
- if an I/O error occurs- Since:
- 2.0
- See Also:
-
map
Deprecated.Fully maps a file in to memory as perFileChannel.map(java.nio.channels.FileChannel.MapMode, long, long)
using the requestedFileChannel.MapMode
.Files are mapped from offset 0 to its length.
This only works for files invalid input: '<'=
Integer.MAX_VALUE
bytes.- Parameters:
file
- the file to mapmode
- the mode to use when mappingfile
- Returns:
- a buffer reflecting
file
- Throws:
FileNotFoundException
- if thefile
does not existIOException
- if an I/O error occurs- Since:
- 2.0
- See Also:
-
map
public static MappedByteBuffer map(File file, FileChannel.MapMode mode, long size) throws FileNotFoundException, IOException Deprecated.Maps a file in to memory as perFileChannel.map(java.nio.channels.FileChannel.MapMode, long, long)
using the requestedFileChannel.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 requestedsize
. Thus this method is useful for creating memory mapped files which do not yet exist.This only works for files invalid input: '<'=
Integer.MAX_VALUE
bytes.- Parameters:
file
- the file to mapmode
- the mode to use when mappingfile
- Returns:
- a buffer reflecting
file
- Throws:
IOException
- if an I/O error occursFileNotFoundException
- Since:
- 2.0
- See Also:
-
simplifyPath
Deprecated.Returns the lexically cleaned form of the path name, usually (but not always) equivalent to the original. The following heuristics are used:- empty string becomes .
- . stays as .
- fold out ./
- fold out ../ when possible
- collapse multiple slashes
- delete trailing slashes (unless the path is just "/")
These heuristics do not always match the behavior of the filesystem. In particular, consider the path
a/../b
, whichsimplifyPath
will change tob
. Ifa
is a symlink tox
,a/../b
may refer to a sibling ofx
, rather than the sibling ofa
referred to byb
.- Since:
- 11.0
-
getFileExtension
Deprecated.Returns the file extension for the given file name, or the empty string if the file has no extension. The result does not include the '.
'.- Since:
- 11.0
-
getNameWithoutExtension
Deprecated.Returns the file name without its file extension or path. This is similar to thebasename
unix command. The result does not include the '.
'.- Parameters:
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.- Returns:
- The file name without its path or extension.
- Since:
- 14.0
-
fileTreeTraverser
Deprecated.Returns aTreeTraverser
instance forFile
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.- Since:
- 15.0
-
isDirectory
Deprecated.Returns a predicate that returns the result ofFile.isDirectory()
on input files.- Since:
- 15.0
-
isFile
Deprecated.Returns a predicate that returns the result ofFile.isFile()
on input files.- Since:
- 15.0
-