Top Description Inners Fields Constructors Methods
java.nio.file

public final Class Files

extends Object
Class Inheritance
Imports
java.io.BufferedReader, .BufferedWriter, .Closeable, .File, .IOException, .InputStream, .InputStreamReader, .OutputStream, .OutputStreamWriter, .Reader, .UncheckedIOException, .Writer, java.nio.channels.Channels, .FileChannel, .SeekableByteChannel, java.nio.charset.Charset, .CharsetDecoder, .CharsetEncoder, .StandardCharsets, java.nio.file.attribute.BasicFileAttributeView, .BasicFileAttributes, .DosFileAttributes, .FileAttribute, .FileAttributeView, .FileOwnerAttributeView, .FileStoreAttributeView, .FileTime, .PosixFileAttributeView, .PosixFileAttributes, .PosixFilePermission, .UserPrincipal, java.nio.file.spi.FileSystemProvider, .FileTypeDetector, java.security.AccessController, .PrivilegedAction, java.util.ArrayList, .Arrays, .Collections, .EnumSet, .HashSet, .Iterator, .List, .Map, .Objects, .ServiceLoader, .Set, .Spliterator, .Spliterators, java.util.function.BiPredicate, java.util.stream.Stream, .StreamSupport, jdk.internal.util.ArraysSupport, sun.nio.ch.FileChannelImpl, sun.nio.cs.UTF_8, sun.nio.fs.AbstractFileSystemProvider

This class consists exclusively of static methods that operate on files, directories, or other types of files.

In most cases, the methods defined here will delegate to the associated file system provider to perform the file operations.

Since
1.7

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
private static class

Field Summary

Modifier and TypeField and Description
private static final int
private static final Set<OpenOption>
private static final JavaLangAccess

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
private static Runnable
asUncheckedRunnable(Closeable c)

Convert a Closeable to a Runnable by converting checked IOException to UncheckedIOException

public static Path

Returns:

the path to the target file
copy
(Path
the path to the file to copy
source
,
Path
the path to the target file (may be associated with a different provider to the source path)
target
,
CopyOption...
options specifying how the copy should be done
options
)

Copy a file to a target file.

public static long

Returns:

the number of bytes read or written
copy
(InputStream
the input stream to read from
in
,
Path
the path to the file
target
,
CopyOption...
options specifying how the copy should be done
options
)

Copies all bytes from an input stream to a file.

public static long

Returns:

the number of bytes read or written
copy
(Path
the path to the file
source
,
OutputStream
the output stream to write to
out
)

Copies all bytes from a file to an output stream.

private static void
createAndCheckIsDirectory(Path dir, FileAttribute<?>... attrs)

Used by createDirectories to attempt to create a directory.

private static Stream<String>
public static Path

Returns:

the directory
createDirectories
(Path
the directory to create
dir
,
FileAttribute<?>...
an optional list of file attributes to set atomically when creating the directory
attrs
)

Creates a directory by creating all nonexistent parent directories first.

public static Path

Returns:

the directory
createDirectory
(Path
the directory to create
dir
,
FileAttribute<?>...
an optional list of file attributes to set atomically when creating the directory
attrs
)

Creates a new directory.

public static Path

Returns:

the file
createFile
(Path
the path to the file to create
path
,
FileAttribute<?>...
an optional list of file attributes to set atomically when creating the file
attrs
)

Creates a new and empty file, failing if the file already exists.

private static Stream<String>
public static Path

Returns:

the path to the newly created directory that did not exist before this method was invoked
createTempDirectory
(Path
the path to directory in which to create the directory
dir
,
String
the prefix string to be used in generating the directory's name; may be null
prefix
,
FileAttribute<?>...
an optional list of file attributes to set atomically when creating the directory
attrs
)

Creates a new directory in the specified directory, using the given prefix to generate its name.

public static Path

Returns:

the path to the newly created directory that did not exist before this method was invoked
createTempDirectory
(String
the prefix string to be used in generating the directory's name; may be null
prefix
,
FileAttribute<?>...
an optional list of file attributes to set atomically when creating the directory
attrs
)

Creates a new directory in the default temporary-file directory, using the given prefix to generate its name.

public static Path

Returns:

the path to the newly created file that did not exist before this method was invoked
createTempFile
(Path
the path to directory in which to create the file
dir
,
String
the prefix string to be used in generating the file's name; may be null
prefix
,
String
the suffix string to be used in generating the file's name; may be null, in which case ".tmp" is used
suffix
,
FileAttribute<?>...
an optional list of file attributes to set atomically when creating the file
attrs
)

Creates a new empty file in the specified directory, using the given prefix and suffix strings to generate its name.

public static Path

Returns:

the path to the newly created file that did not exist before this method was invoked
createTempFile
(String
the prefix string to be used in generating the file's name; may be null
prefix
,
String
the suffix string to be used in generating the file's name; may be null, in which case ".tmp" is used
suffix
,
FileAttribute<?>...
an optional list of file attributes to set atomically when creating the file
attrs
)

Creates an empty file in the default temporary-file directory, using the given prefix and suffix to generate its name.

public static void
delete(Path
the path to the file to delete
path
)

Deletes a file.

public static boolean

Returns:

true if the file was deleted by this method; false if the file could not be deleted because it did not exist
deleteIfExists
(Path
the path to the file to delete
path
)

Deletes a file if it exists.

public static boolean

Returns:

true if the file exists; false if the file does not exist or its existence cannot be determined.
exists
(Path
the path to the file to test
path
,
LinkOption...
options indicating how symbolic links are handled
options
)

Tests whether a file exists.

public static Stream<Path>

Returns:

the Stream of Path
find
(Path
the starting file
start
,
int
the maximum number of directory levels to search
maxDepth
,
BiPredicate<Path, BasicFileAttributes>
the function used to decide whether a file should be included in the returned stream
matcher
,
FileVisitOption...
options to configure the traversal
options
)

Returns a Stream that is lazily populated with Path by searching for files in a file tree rooted at a given starting file.

public static Object

Returns:

the attribute value
getAttribute
(Path
the path to the file
path
,
String
the attribute to read
attribute
,
LinkOption...
options indicating how symbolic links are handled
options
)

Reads the value of a file attribute.

public static <
The FileAttributeView type
V extends FileAttributeView
>
V

Returns:

a file attribute view of the specified type, or null if the attribute view type is not available
getFileAttributeView
(Path
the path to the file
path
,
Class<V>
the Class object corresponding to the file attribute view
type
,
LinkOption...
options indicating how symbolic links are handled
options
)

Returns a file attribute view of a given type.

public static FileStore

Returns:

the file store where the file is stored
getFileStore
(Path
the path to the file
path
)

Returns the FileStore representing the file store where a file is located.

public static FileTime

Returns:

a FileTime representing the time the file was last modified, or an implementation specific default when a time stamp to indicate the time of last modification is not supported by the file system
getLastModifiedTime
(Path
the path to the file
path
,
LinkOption...
options indicating how symbolic links are handled
options
)

Returns a file's last modified time.

public static UserPrincipal

Returns:

A user principal representing the owner of the file
getOwner
(Path
The path to the file
path
,
LinkOption...
options indicating how symbolic links are handled
options
)

Returns the owner of a file.

public static Set<PosixFilePermission>

Returns:

the file permissions
getPosixFilePermissions
(Path
the path to the file
path
,
LinkOption...
options indicating how symbolic links are handled
options
)

Returns a file's POSIX file permissions.

private static boolean
isAccessible(Path path, AccessMode... modes)

Used by isReadable, isWritable, isExecutable to test access to a file.

public static boolean

Returns:

true if the file is a directory; false if the file does not exist, is not a directory, or it cannot be determined if the file is a directory or not.
isDirectory
(Path
the path to the file to test
path
,
LinkOption...
options indicating how symbolic links are handled
options
)

Tests whether a file is a directory.

public static boolean

Returns:

true if the file exists and is executable; false if the file does not exist, execute access would be denied because the Java virtual machine has insufficient privileges, or access cannot be determined
isExecutable
(Path
the path to the file to check
path
)

Tests whether a file is executable.

public static boolean

Returns:

true if the file is considered hidden
isHidden
(Path
the path to the file to test
path
)

Tells whether or not a file is considered hidden.

public static boolean

Returns:

true if the file exists and is readable; false if the file does not exist, read access would be denied because the Java virtual machine has insufficient privileges, or access cannot be determined
isReadable
(Path
the path to the file to check
path
)

Tests whether a file is readable.

public static boolean

Returns:

true if the file is a regular file; false if the file does not exist, is not a regular file, or it cannot be determined if the file is a regular file or not.
isRegularFile
(Path
the path to the file
path
,
LinkOption...
options indicating how symbolic links are handled
options
)

Tests whether a file is a regular file with opaque content.

public static boolean

Returns:

true if, and only if, the two paths locate the same file
isSameFile
(Path
one path to the file
path
,
Path
the other path
path2
)

Tests if two paths locate the same file.

public static boolean

Returns:

true if the file exists and is writable; false if the file does not exist, write access would be denied because the Java virtual machine has insufficient privileges, or access cannot be determined
isWritable
(Path
the path to the file to check
path
)

Tests whether a file is writable.

public static Stream<String>

Returns:

the lines from the file as a Stream
lines
(Path
the path to the file
path
,
Charset
the charset to use for decoding
cs
)

Read all lines from a file as a Stream.

public static Stream<String>

Returns:

the lines from the file as a Stream
lines
(Path
the path to the file
path
)

Read all lines from a file as a Stream.

public static Stream<Path>

Returns:

The Stream describing the content of the directory
list
(Path
The path to the directory
dir
)

Returns a lazily populated Stream, the elements of which are the entries in the directory.

public static long

Returns:

the position of the first mismatch or -1L if no mismatch
mismatch
(Path
the path to the first file
path
,
Path
the path to the second file
path2
)

Finds and returns the position of the first mismatched byte in the content of two files, or -1L if there is no mismatch.

public static Path

Returns:

the path to the target file
move
(Path
the path to the file to move
source
,
Path
the path to the target file (may be associated with a different provider to the source path)
target
,
CopyOption...
options specifying how the move should be done
options
)

Move or rename a file to a target file.

public static BufferedReader

Returns:

a new buffered reader, with default buffer size, to read text from the file
newBufferedReader
(Path
the path to the file
path
,
Charset
the charset to use for decoding
cs
)

Opens a file for reading, returning a BufferedReader that may be used to read text from the file in an efficient manner.

public static BufferedReader

Returns:

a new buffered reader, with default buffer size, to read text from the file
newBufferedReader
(Path
the path to the file
path
)

Opens a file for reading, returning a BufferedReader to read text from the file in an efficient manner.

public static BufferedWriter

Returns:

a new buffered writer, with default buffer size, to write text to the file
newBufferedWriter
(Path
the path to the file
path
,
Charset
the charset to use for encoding
cs
,
OpenOption...
options specifying how the file is opened
options
)

Opens or creates a file for writing, returning a BufferedWriter that may be used to write text to the file in an efficient manner.

public static BufferedWriter

Returns:

a new buffered writer, with default buffer size, to write text to the file
newBufferedWriter
(Path
the path to the file
path
,
OpenOption...
options specifying how the file is opened
options
)

Opens or creates a file for writing, returning a BufferedWriter to write text to the file in an efficient manner.

public static SeekableByteChannel

Returns:

a new seekable byte channel
newByteChannel
(Path
the path to the file to open or create
path
,
Set<? extends OpenOption>
options specifying how the file is opened
options
,
FileAttribute<?>...
an optional list of file attributes to set atomically when creating the file
attrs
)

Opens or creates a file, returning a seekable byte channel to access the file.

public static SeekableByteChannel

Returns:

a new seekable byte channel
newByteChannel
(Path
the path to the file to open or create
path
,
OpenOption...
options specifying how the file is opened
options
)

Opens or creates a file, returning a seekable byte channel to access the file.

public static DirectoryStream<Path>

Returns:

a new and open DirectoryStream object
newDirectoryStream
(Path
the path to the directory
dir
)

Opens a directory, returning a DirectoryStream to iterate over all entries in the directory.

public static DirectoryStream<Path>

Returns:

a new and open DirectoryStream object
newDirectoryStream
(Path
the path to the directory
dir
,
String
the glob pattern
glob
)

Opens a directory, returning a DirectoryStream to iterate over the entries in the directory.

public static DirectoryStream<Path>

Returns:

a new and open DirectoryStream object
newDirectoryStream
(Path
the path to the directory
dir
,
DirectoryStream.Filter<? super Path>
the directory stream filter
filter
)

Opens a directory, returning a DirectoryStream to iterate over the entries in the directory.

public static InputStream

Returns:

a new input stream
newInputStream
(Path
the path to the file to open
path
,
OpenOption...
options specifying how the file is opened
options
)

Opens a file, returning an input stream to read from the file.

public static OutputStream

Returns:

a new output stream
newOutputStream
(Path
the path to the file to open or create
path
,
OpenOption...
options specifying how the file is opened
options
)

Opens or creates a file, returning an output stream that may be used to write bytes to the file.

public static boolean

Returns:

true if the file does not exist; false if the file exists or its existence cannot be determined
notExists
(Path
the path to the file to test
path
,
LinkOption...
options indicating how symbolic links are handled
options
)

Tests whether the file located by this path does not exist.

public static String

Returns:

The content type of the file, or null if the content type cannot be determined
probeContentType
(Path
the path to the file to probe
path
)

Probes the content type of a file.

private static FileSystemProvider
provider(Path path)

Returns the FileSystemProvider to delegate to.

private static byte[]

Returns:

a byte array containing the bytes read from the file
read
(InputStream
the input stream to read from
source
,
int
the initial size of the byte array to allocate
initialSize
)

Reads all the bytes from an input stream.

public static byte[]

Returns:

a byte array containing the bytes read from the file
readAllBytes
(Path
the path to the file
path
)

Reads all the bytes from a file.

public static List<String>

Returns:

the lines from the file as a List; whether the List is modifiable or not is implementation dependent and therefore not specified
readAllLines
(Path
the path to the file
path
,
Charset
the charset to use for decoding
cs
)

Read all lines from a file.

public static List<String>

Returns:

the lines from the file as a List; whether the List is modifiable or not is implementation dependent and therefore not specified
readAllLines
(Path
the path to the file
path
)

Read all lines from a file.

public static <
The BasicFileAttributes type
A extends BasicFileAttributes
>
A

Returns:

the file attributes
readAttributes
(Path
the path to the file
path
,
Class<A>
the Class of the file attributes required to read
type
,
LinkOption...
options indicating how symbolic links are handled
options
)

Reads a file's attributes as a bulk operation.

public static Map<String, Object>

Returns:

a map of the attributes returned; The map's keys are the attribute names, its values are the attribute values
readAttributes
(Path
the path to the file
path
,
String
the attributes to read
attributes
,
LinkOption...
options indicating how symbolic links are handled
options
)

Reads a set of file attributes as a bulk operation.

public static String

Returns:

a String containing the content read from the file
readString
(Path
the path to the file
path
)

Reads all content from a file into a string, decoding from bytes to characters using the UTF-8 charset.

public static String

Returns:

a String containing the content read from the file
readString
(Path
the path to the file
path
,
Charset
the charset to use for decoding
cs
)

Reads all characters from a file into a string, decoding from bytes to characters using the specified charset.

public static Path

Returns:

the given path
setAttribute
(Path
the path to the file
path
,
String
the attribute to set
attribute
,
Object
the attribute value
value
,
LinkOption...
options indicating how symbolic links are handled
options
)

Sets the value of a file attribute.

public static Path

Returns:

the given path
setLastModifiedTime
(Path
the path to the file
path
,
FileTime
the new last modified time
time
)

Updates a file's last modified time attribute.

public static Path

Returns:

The given path
setOwner
(Path
The path to the file
path
,
UserPrincipal
The new file owner
owner
)

Updates the file owner.

public static Path

Returns:

The given path
setPosixFilePermissions
(Path
The path to the file
path
,
Set<PosixFilePermission>
The new set of permissions
perms
)

Sets a file's POSIX permissions.

public static long

Returns:

the file size, in bytes
size
(Path
the path to the file
path
)

Returns the size of a file (in bytes).

public static Stream<Path>

Returns:

the Stream of Path
walk
(Path
the starting file
start
,
int
the maximum number of directory levels to visit
maxDepth
,
FileVisitOption...
options to configure the traversal
options
)

Returns a Stream that is lazily populated with Path by walking the file tree rooted at a given starting file.

public static Stream<Path>

Returns:

the Stream of Path
walk
(Path
the starting file
start
,
FileVisitOption...
options to configure the traversal
options
)

Returns a Stream that is lazily populated with Path by walking the file tree rooted at a given starting file.

public static Path

Returns:

the starting file
walkFileTree
(Path
the starting file
start
,
Set<FileVisitOption>
options to configure the traversal
options
,
int
the maximum number of directory levels to visit
maxDepth
,
FileVisitor<? super Path>
the file visitor to invoke for each file
visitor
)

Walks a file tree.

public static Path

Returns:

the starting file
walkFileTree
(Path
the starting file
start
,
FileVisitor<? super Path>
the file visitor to invoke for each file
visitor
)

Walks a file tree.

public static Path

Returns:

the path
write
(Path
the path to the file
path
,
byte[]
the byte array with the bytes to write
bytes
,
OpenOption...
options specifying how the file is opened
options
)

Writes bytes to a file.

public static Path

Returns:

the path
write
(Path
the path to the file
path
,
Iterable<? extends CharSequence>
an object to iterate over the char sequences
lines
,
Charset
the charset to use for encoding
cs
,
OpenOption...
options specifying how the file is opened
options
)

Write lines of text to a file.

public static Path

Returns:

the path
write
(Path
the path to the file
path
,
Iterable<? extends CharSequence>
an object to iterate over the char sequences
lines
,
OpenOption...
options specifying how the file is opened
options
)

Write lines of text to a file.

public static Path

Returns:

the path
writeString
(Path
the path to the file
path
,
CharSequence
the CharSequence to be written
csq
,
OpenOption...
options specifying how the file is opened
options
)

Write a CharSequence to a file.

public static Path

Returns:

the path
writeString
(Path
the path to the file
path
,
CharSequence
the CharSequence to be written
csq
,
Charset
the charset to use for encoding
cs
,
OpenOption...
options specifying how the file is opened
options
)

Write a CharSequence to a file.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait