| Modifier and Type | Method and Description |
|---|---|
static void |
append(CharSequence
Appends a character sequence (such as a string) to a file using the given character set.
|
static ByteSink |
asByteSink(File
Returns a new
ByteSink for writing bytes to the given file.
|
static ByteSource |
asByteSource(File
Returns a new
ByteSource for reading bytes from the given file.
|
static CharSink |
asCharSink(File
Returns a new
CharSink for writing character data to the given file using the given character set.
|
static CharSource |
asCharSource(File
Returns a new
CharSource for reading character data from the given file using the given character set.
|
static void |
copy(File
Copies all characters from a file to an appendable object, using the given character set.
|
static void |
copy(File
Copies all the bytes from one file to another.
|
static void |
copy(File
Copies all bytes from a file to an output stream.
|
static void |
createParentDirs(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
Returns true if the files contains the same bytes.
|
static TreeTraverser |
fileTreeTraverser()
Returns a
TreeTraverser instance for
File trees.
|
static String |
getFileExtension(String
Returns the
file extension for the given file name, or the empty string if the file has no extension.
|
static String |
getNameWithoutExtension(String
Returns the file name without its
file extension or path.
|
static HashCode |
hash(File
Computes the hash code of the
file using
hashFunction.
|
static Predicate |
isDirectory()
Returns a predicate that returns the result of
File on input files.
|
static Predicate |
isFile()
Returns a predicate that returns the result of
File on input files.
|
static MappedByteBuffer |
map(File
Fully maps a file read-only in to memory as per
FileChannel.
|
static MappedByteBuffer |
map(File
Fully maps a file in to memory as per
FileChannel using the requested
FileChannel.MapMode.
|
static MappedByteBuffer |
map(File
Maps a file in to memory as per
FileChannel using the requested
FileChannel.MapMode.
|
static void |
move(File
Moves a file from one path to another.
|
static BufferedReader |
newReader(File
Returns a buffered reader that reads from a file using the given character set.
|
static BufferedWriter |
newWriter(File
Returns a buffered writer that writes to a file using the given character set.
|
static <T> T |
readBytes(File
Process the bytes of a file.
|
static String |
readFirstLine(File
Reads the first line from a file.
|
static List |
readLines(File
Reads all of the lines from a file.
|
static <T> T |
readLines(File
Streams lines from a
File, stopping when our callback returns false, or we have read all of the lines.
|
static String |
simplifyPath(String
Returns the lexically cleaned form of the path name,
usually (but not always) equivalent to the original.
|
static byte[] |
toByteArray(File
Reads all bytes from a file into a byte array.
|
static String |
toString(File
Reads all characters from a file into a
String, using the given character set.
|
static void |
touch(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
Overwrites a file with the contents of a byte array.
|
static void |
write(CharSequence
Writes a character sequence (such as a string) to a file using the given character set.
|
public static BufferedReadernewReader(File file, Charset charset) throws FileNotFoundException
file - the file to read from
charset - the charset used to decode the input stream; see
Charsets for helpful predefined constants
FileNotFoundException
public static BufferedWriternewWriter(File file, Charset charset) throws FileNotFoundException
file - the file to write to
charset - the charset used to encode the output stream; see
Charsets for helpful predefined constants
FileNotFoundException
public static ByteSourceasByteSource(File file)
ByteSource for reading bytes from the given file.
public static ByteSinkasByteSink(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 CharSourceasCharSource(File file, Charset charset)
CharSource for reading character data from the given file using the given character set.
public static CharSinkasCharSink(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(Filefile) 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 occurs
public static StringtoString(File file, Charset charset) throws IOException
String, using the given character set.
file - the file to read from
charset - the charset used to decode the input stream; see
Charsets for helpful predefined constants
IOException - if an I/O error occurs
public static void write(byte[] from,
File to)
throws IOException
from - the bytes to write
to - the destination file
IOException - if an I/O error occurs
public static void copy(Filefrom, OutputStream to) throws IOException
from - the source file
to - the output stream
IOException - if an I/O error occurs
public static void copy(Filefrom, File to) throws 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 file
to - the destination file
IOException - if an I/O error occurs
IllegalArgumentException - if
from.equals(to)
public static void write(CharSequencefrom, File to, Charset charset) throws IOException
from - the character sequence to write
to - the destination file
charset - the charset used to encode the output stream; see
Charsets for helpful predefined constants
IOException - if an I/O error occurs
public static void append(CharSequencefrom, File to, Charset charset) throws IOException
from - the character sequence to append
to - the destination file
charset - the charset used to encode the output stream; see
Charsets for helpful predefined constants
IOException - if an I/O error occurs
public static void copy(Filefrom, Charset charset, Appendable to) throws IOException
from - the source file
charset - the charset used to decode the input stream; see
Charsets for helpful predefined constants
to - the appendable object
IOException - if an I/O error occurs
public static boolean equal(Filefile1, File file2) throws IOException
IOException - if an I/O error occurs
public static FilecreateTempDir()
java.io.tmpdir system property), and returns its name.
Use this method instead of File 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 created
public static void touch(Filefile) throws IOException
file - the file to create or update
IOException - if an I/O error occurs
public static void createParentDirs(Filefile) 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(Filefrom, 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.
from - the source file
to - the destination file
IOException - if an I/O error occurs
IllegalArgumentException - if
from.equals(to)
public static StringreadFirstLine(File file, Charset charset) throws IOException
file - the file to read from
charset - the charset used to decode the input stream; see
Charsets for helpful predefined constants
IOException - if an I/O error occurs
public 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().
file - the file to read from
charset - 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 occurs
public static <T> T readLines(Filefile, 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 from
charset - the charset used to decode the input stream; see
Charsets for helpful predefined constants
callback - the
LineProcessor to use to handle the lines
IOException - if an I/O error occurs
public static <T> T readBytes(Filefile, ByteProcessor <T> processor) throws IOException
(If this seems too complicated, maybe you're looking for toByteArray(java.io.File).)
file - the file to read
processor - the object to which the bytes of the file are passed.
IOException - if an I/O error occurs
public static HashCodehash(File file, HashFunction hashFunction) throws IOException
file using
hashFunction.
file - the file to read
hashFunction - the hash function to use to hash the data
HashCode of all of the bytes in the file
IOException - if an I/O error occurs
public static MappedByteBuffermap(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 bytes.
file - the file to map
file
FileNotFoundException - if the
file does not exist
IOException - if an I/O error occurs
FileChannel.map(MapMode, long, long)
public static MappedByteBuffermap(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 bytes.
file - the file to map
mode - the mode to use when mapping
file
file
FileNotFoundException - if the
file does not exist
IOException - if an I/O error occurs
FileChannel.map(MapMode, long, long)
public static MappedByteBuffermap(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 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 bytes.
file - the file to map
mode - the mode to use when mapping
file
file
IOException - if an I/O error occurs
FileNotFoundException
FileChannel.map(MapMode, long, long)
public static StringsimplifyPath(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 StringgetFileExtension(String fullName)
.'.
public static StringgetNameWithoutExtension(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 TreeTraverser<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 Predicate<File > isDirectory()
File.isDirectory() on input files.
public static Predicate<File > isFile()
File.isFile() on input files.