Class TruffleFile
- Since:
- 19.0
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic final class
Represents a file's attribute.static final class
A view over file's attributes values obtained bygetAttributes
.static interface
A detector for findingfile
's MIME type and encoding. -
Field Summary
Modifier and TypeFieldDescriptionstatic final TruffleFile.AttributeDescriptor
<FileTime> The file's creation time.static final TruffleFile.AttributeDescriptor
<Boolean> Represents the file a directory.static final TruffleFile.AttributeDescriptor
<Boolean> Represents the file a special file (device, named pipe).static final TruffleFile.AttributeDescriptor
<Boolean> Represents the file a regular file.static final TruffleFile.AttributeDescriptor
<Boolean> Represents the file a symbolic link.static final TruffleFile.AttributeDescriptor
<FileTime> The file's last access time.static final TruffleFile.AttributeDescriptor
<FileTime> The file's last modified time.static final TruffleFile.AttributeDescriptor
<Long> The file's size in bytes.static final TruffleFile.AttributeDescriptor
<FileTime> The file's last status change time.static final TruffleFile.AttributeDescriptor
<Long> The id of a device containing the file.static final TruffleFile.AttributeDescriptor
<Integer> The group id of file owner.static final TruffleFile.AttributeDescriptor
<GroupPrincipal> The group owner of the file.static final TruffleFile.AttributeDescriptor
<Long> The file's inode number.static final TruffleFile.AttributeDescriptor
<Integer> The file's mode containing the protection and file type bits.static final TruffleFile.AttributeDescriptor
<Integer> The number of hard links.static final TruffleFile.AttributeDescriptor
<UserPrincipal> The owner of the file.static final TruffleFile.AttributeDescriptor
<Set<PosixFilePermission>> The file's Posix permissions.static final TruffleFile.AttributeDescriptor
<Long> The id of a device represented by the file.static final TruffleFile.AttributeDescriptor
<Integer> The user id of file owner. -
Method Summary
Modifier and TypeMethodDescriptionvoid
copy
(TruffleFile target, CopyOption... options) Copies the file.void
createDirectories
(FileAttribute<?>... attributes) Creates a directory and all nonexistent parent directories.void
createDirectory
(FileAttribute<?>... attributes) Creates a new directory.void
createFile
(FileAttribute<?>... attributes) Creates a new empty file.void
createLink
(TruffleFile target) Creates a new link to an existing target (optional operation).void
createSymbolicLink
(TruffleFile target, FileAttribute<?>... attrs) Creates a symbolic link to a target (optional operation).void
delete()
Deletes the file.Detects thefile
MIME type.boolean
endsWith
(TruffleFile other) Tests if thisTruffleFile
path ends with the givenTruffleFile
path.boolean
Tests if thisTruffleFile
path ends with the given path.boolean
boolean
exists
(LinkOption... options) Tests existence of a file.Resolves thisTruffleFile
to absoluteTruffleFile
.<T> T
getAttribute
(TruffleFile.AttributeDescriptor<T> attribute, LinkOption... linkOptions) Reads a single file's attribute.getAttributes
(Collection<? extends TruffleFile.AttributeDescriptor<?>> attributes, LinkOption... linkOptions) Reads file's attributes as a bulk operation.getCanonicalFile
(LinkOption... options) Returns aTruffleFile
representing the real (canonical) path of an existing file.getCreationTime
(LinkOption... options) Returns the creation time.getGroup
(LinkOption... options) Returns the group owner of the file.getLastAccessTime
(LinkOption... options) Returns the last access time.getLastModifiedTime
(LinkOption... options) Returns the last modified time.Deprecated.getName()
Returns the name of thisTruffleFile
.getOwner
(LinkOption... options) Returns the owner of the file.Returns a parentTruffleFile
or null when the file does not have a parent.getPath()
Returns the string representation of thisTruffleFile
.getPosixPermissions
(LinkOption... linkOptions) Returns the file's Posix permissions.int
hashCode()
boolean
Tests if thisTruffleFile
's path is absolute.boolean
isDirectory
(LinkOption... options) Tests if a file is a directory.boolean
Tests if a file is executable.boolean
Tests if a file is readable.boolean
isRegularFile
(LinkOption... options) Tests if a file is a regular file.boolean
isSameFile
(TruffleFile other, LinkOption... options) Tests if this and the givenTruffleFile
refer to the same physical file.boolean
Tests if a file is a symbolic link.boolean
Tests if a file is writable.list()
Returns a collection ofTruffleFile
s in the directory denoted by thisTruffleFile
.void
move
(TruffleFile target, CopyOption... options) Moves or renames the file.Opens a file for reading returning aBufferedReader
to access the file content.newBufferedReader
(Charset charset) Opens a file for reading returning aBufferedReader
to access the file content.newBufferedWriter
(Charset charset, OpenOption... options) Opens a file for writing returning anBufferedWriter
.newBufferedWriter
(OpenOption... options) Opens a file for writing returning anBufferedWriter
.newByteChannel
(Set<? extends OpenOption> options, FileAttribute<?>... attributes) Opens or creates a file returning aSeekableByteChannel
to access the file content.Opens a directory, returning aDirectoryStream
to iterate over all entries in the directory.newInputStream
(OpenOption... options) Opens a file for reading returning anInputStream
to access the file content.newOutputStream
(OpenOption... options) Opens a file for writing returning anOutputStream
.Returns aTruffleFile
with removed redundant name elements in it's path.byte[]
Reads a file content as bytes.Reads the target of a symbolic link.relativize
(TruffleFile other) Resolves given string path representation against thisTruffleFile
.resolveSibling
(String name) Resolves given string path representation against the parent of thisTruffleFile
.<T> void
setAttribute
(TruffleFile.AttributeDescriptor<T> attribute, T value, LinkOption... linkOptions) Sets a single file's attribute.void
setCreationTime
(FileTime time, LinkOption... options) Sets the file's creation time.void
setLastAccessTime
(FileTime time, LinkOption... options) Sets the file's last access time.void
setLastModifiedTime
(FileTime time, LinkOption... options) Sets the file's last modified time.void
setPosixPermissions
(Set<? extends PosixFilePermission> permissions, LinkOption... linkOptions) Sets the file's Posix permissions.long
size
(LinkOption... options) Returns the size of a file.boolean
startsWith
(TruffleFile other) Tests if thisTruffleFile
path starts with the givenTruffleFile
path.boolean
startsWith
(String other) Tests if thisTruffleFile
path starts with the given path.Returns a relativeURI
representation of non absoluteTruffleFile
.toString()
toUri()
Returns the absoluteURI
representation of thisTruffleFile
.void
visit
(FileVisitor<TruffleFile> visitor, int maxDepth, FileVisitOption... options) Visits thisTruffleFile
file tree.
-
Field Details
-
LAST_MODIFIED_TIME
The file's last modified time. Supported by all filesystems.- Since:
- 19.0
-
LAST_ACCESS_TIME
The file's last access time. Supported by all filesystems.- Since:
- 19.0
-
CREATION_TIME
The file's creation time. Supported by all filesystems.- Since:
- 19.0
-
IS_REGULAR_FILE
Represents the file a regular file. Supported by all filesystems.- Since:
- 19.0
-
IS_DIRECTORY
Represents the file a directory. Supported by all filesystems.- Since:
- 19.0
-
IS_SYMBOLIC_LINK
Represents the file a symbolic link. Supported by all filesystems.- Since:
- 19.0
-
IS_OTHER
Represents the file a special file (device, named pipe). Supported by all filesystems.- Since:
- 19.0
-
SIZE
The file's size in bytes. Supported by all filesystems.- Since:
- 19.0
-
UNIX_OWNER
The owner of the file. Supported only by UNIX native filesystem.- Since:
- 19.0
-
UNIX_GROUP
The group owner of the file. Supported only by UNIX native filesystem.- Since:
- 19.0
-
UNIX_PERMISSIONS
The file's Posix permissions. Supported only by UNIX native filesystem.- Since:
- 19.0
-
UNIX_MODE
The file's mode containing the protection and file type bits. Supported only by UNIX native filesystem.- Since:
- 19.0
-
UNIX_INODE
The file's inode number. Supported only by UNIX native filesystem.- Since:
- 19.0
-
UNIX_DEV
The id of a device containing the file. Supported only by UNIX native filesystem.- Since:
- 19.0
-
UNIX_RDEV
The id of a device represented by the file. Supported only by UNIX native filesystem.- Since:
- 19.0
-
UNIX_NLINK
The number of hard links. Supported only by UNIX native filesystem.- Since:
- 19.0
-
UNIX_UID
The user id of file owner. Supported only by UNIX native filesystem.- Since:
- 19.0
-
UNIX_GID
The group id of file owner. Supported only by UNIX native filesystem.- Since:
- 19.0
-
UNIX_CTIME
The file's last status change time. Supported only by UNIX native filesystem.- Since:
- 19.0
-
-
Method Details
-
exists
Tests existence of a file.- Parameters:
options
- the options determining how the symbolic links should be handled- Returns:
true
if the file exists- Throws:
SecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
isReadable
public boolean isReadable()Tests if a file is readable. Checks if the file exists and this Java virtual machine has enough privileges to read the file.- Returns:
true
if the file exists and is readable- Throws:
SecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
isWritable
public boolean isWritable()Tests if a file is writable. Checks if the file exists and this Java virtual machine has enough privileges to write to the file.- Returns:
true
if the file exists and is writable- Throws:
SecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
isExecutable
public boolean isExecutable()Tests if a file is executable. Checks if the file exists and this Java virtual machine has enough privileges to execute the file.- Returns:
true
if the file exists and is executable- Throws:
SecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
isDirectory
Tests if a file is a directory. Checks if the file exists and is a directory.- Parameters:
options
- the options determining how the symbolic links should be handled, by default the symbolic links are followed.- Returns:
true
if the file exists and is a directory- Throws:
SecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
isRegularFile
Tests if a file is a regular file. Checks if the file exists and is a regular file.- Parameters:
options
- the options determining how the symbolic links should be handled, by default the symbolic links are followed.- Returns:
true
if the file exists and is a regular file- Throws:
SecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
isSymbolicLink
public boolean isSymbolicLink()Tests if a file is a symbolic link. Checks if the file exists and is a symbolic link.- Returns:
true
if the file exists and is a symbolic link- Throws:
SecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
isAbsolute
public boolean isAbsolute()Tests if thisTruffleFile
's path is absolute.- Returns:
true
if the file path is absolute- Since:
- 19.0
-
getName
Returns the name of thisTruffleFile
.- Returns:
- the name of file or directory denoted by this
TruffleFile
, ornull
if the file is a root directory - Since:
- 19.0
-
getPath
Returns the string representation of thisTruffleFile
.- Returns:
- the path of this
TruffleFile
- Since:
- 19.0
-
toUri
Returns the absoluteURI
representation of thisTruffleFile
.- Returns:
- the absolute
URI
representing theTruffleFile
- Throws:
SecurityException
- if theFileSystem
denied a resolution of an absolute path- Since:
- 19.0
-
toRelativeUri
Returns a relativeURI
representation of non absoluteTruffleFile
. If thisTruffleFile
is relative it returns a relativeURI
. For anabsolute
TruffleFile
it returns an absoluteURI
.- Returns:
- the
URI
representing theTruffleFile
- Since:
- 19.0
-
getAbsoluteFile
Resolves thisTruffleFile
to absoluteTruffleFile
. If thisTruffleFile
is already absolute this method returns thisTruffleFile
without any resolution.- Returns:
- the absolute
TruffleFile
- Throws:
SecurityException
- if theFileSystem
denied a resolution of an absolute path- Since:
- 19.0
-
getCanonicalFile
Returns aTruffleFile
representing the real (canonical) path of an existing file.- Parameters:
options
- the options determining how the symbolic links should be handled- Returns:
- a
TruffleFile
representing the absolute canonical path - Throws:
IOException
- in case of IO errorSecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
getParent
Returns a parentTruffleFile
or null when the file does not have a parent.- Returns:
- the parent
TruffleFile
- Since:
- 19.0
-
resolve
Resolves given string path representation against thisTruffleFile
.- Parameters:
name
- the path to resolve- Returns:
- the resolved
TruffleFile
- Throws:
InvalidPathException
- if the path string contains non valid characters- Since:
- 19.0
-
resolveSibling
Resolves given string path representation against the parent of thisTruffleFile
.- Parameters:
name
- the path to resolve- Returns:
- the resolved
TruffleFile
- Throws:
InvalidPathException
- if the path string contains non valid characters- Since:
- 19.0
-
size
Returns the size of a file.- Parameters:
options
- the options determining how the symbolic links should be handled- Returns:
- the file size in bytes
- Throws:
IOException
- in case of IO errorSecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
getLastModifiedTime
Returns the last modified time.- Parameters:
options
- the options determining how the symbolic links should be handled- Returns:
- the
FileTime
representing the time thisTruffleFile
was last modified - Throws:
IOException
- in case of IO errorSecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
setLastModifiedTime
Sets the file's last modified time.- Parameters:
time
- the new value of the last modified timeoptions
- the options determining how the symbolic links should be handled- Throws:
IOException
- in case of IO errorSecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
getLastAccessTime
Returns the last access time.- Parameters:
options
- the options determining how the symbolic links should be handled- Returns:
- the
FileTime
representing the time thisTruffleFile
was last accessed - Throws:
IOException
- in case of IO errorSecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
setLastAccessTime
Sets the file's last access time.- Parameters:
time
- the new value of the last access timeoptions
- the options determining how the symbolic links should be handled- Throws:
IOException
- in case of IO errorSecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
getCreationTime
Returns the creation time.- Parameters:
options
- the options determining how the symbolic links should be handled- Returns:
- the
FileTime
representing the time thisTruffleFile
was created - Throws:
IOException
- in case of IO errorSecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
setCreationTime
Sets the file's creation time.- Parameters:
time
- the new value of the creation timeoptions
- the options determining how the symbolic links should be handled- Throws:
IOException
- in case of IO errorSecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
list
Returns a collection ofTruffleFile
s in the directory denoted by thisTruffleFile
.- Returns:
- a collection of
TruffleFile
s located in the directory denoted by thisTruffleFile
- Throws:
IOException
- in case of IO errorSecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
newByteChannel
public SeekableByteChannel newByteChannel(Set<? extends OpenOption> options, FileAttribute<?>... attributes) throws IOException Opens or creates a file returning aSeekableByteChannel
to access the file content. In most cases, the returnedSeekableByteChannel
should be closed using try-with-resources construct. When the channel must keep being opened for the lifetime of a context it should beregistered
for automatic close on context dispose.- Parameters:
options
- the options specifying how the file should be openedattributes
- the optional attributes to set atomically when creating the new file- Returns:
- the created
SeekableByteChannel
- Throws:
FileAlreadyExistsException
- ifStandardOpenOption.CREATE_NEW
option is set and a file already exists on given pathIOException
- in case of IO errorUnsupportedOperationException
- if the attributes contain an attribute which cannot be set atomicallyIllegalArgumentException
- in case of invalid options combinationSecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
newInputStream
Opens a file for reading returning anInputStream
to access the file content. In most cases, the returnedInputStream
should be closed using try-with-resources construct. When the stream must keep being opened for the lifetime of a context it should beregistered
for automatic close on context dispose.- Parameters:
options
- the options specifying how the file should be opened- Returns:
- the created
InputStream
- Throws:
IOException
- in case of IO errorIllegalArgumentException
- in case of invalid options combinationSecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
newBufferedReader
Opens a file for reading returning aBufferedReader
to access the file content. In most cases, the returnedBufferedReader
should be closed using try-with-resources construct. When the reader must keep being opened for the lifetime of a context it should beregistered
for automatic close on context dispose.- Parameters:
charset
- the file encoding- Returns:
- the created
BufferedReader
- Throws:
IOException
- in case of IO errorSecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
newBufferedReader
Opens a file for reading returning aBufferedReader
to access the file content. SeenewBufferedReader(Charset)
.- Returns:
- the created
BufferedReader
- Throws:
IOException
- in case of IO errorSecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
readAllBytes
Reads a file content as bytes.- Returns:
- the created
byte[]
- Throws:
IOException
- in case of IO errorOutOfMemoryError
- if an array of a file size cannot be allocatedSecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
newOutputStream
Opens a file for writing returning anOutputStream
. In most cases, the returnedOutputStream
should be closed using try-with-resources construct. When the stream must keep being opened for the lifetime of a context it should beregistered
for automatic close on context dispose.- Parameters:
options
- the options specifying how the file should be opened- Returns:
- the created
OutputStream
- Throws:
IOException
- in case of IO errorIllegalArgumentException
- in case of invalid options combinationSecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
newBufferedWriter
Opens a file for writing returning anBufferedWriter
. In most cases, the returnedBufferedWriter
should be closed using try-with-resources construct. When the writer must keep being opened for the lifetime of a context it should beregistered
for automatic close on context dispose.- Parameters:
charset
- the file encodingoptions
- the options specifying how the file should be opened- Returns:
- the created
BufferedWriter
- Throws:
IOException
- in case of IO errorIllegalArgumentException
- in case of invalid options combinationSecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
newBufferedWriter
Opens a file for writing returning anBufferedWriter
. SeenewBufferedWriter(Charset, OpenOption...)
.- Parameters:
options
- the options specifying how the file should be opened- Returns:
- the created
BufferedWriter
- Throws:
IOException
- in case of IO errorIllegalArgumentException
- in case of invalid options combinationSecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
createFile
Creates a new empty file.- Parameters:
attributes
- the optional attributes to set atomically when creating the new file- Throws:
FileAlreadyExistsException
- if the file already exists on given pathIOException
- in case of IO errorUnsupportedOperationException
- if the attributes contain an attribute which cannot be set atomicallySecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
createDirectory
Creates a new directory.- Parameters:
attributes
- the optional attributes to set atomically when creating the new file- Throws:
FileAlreadyExistsException
- if the file or directory already exists on given pathIOException
- in case of IO errorUnsupportedOperationException
- if the attributes contain an attribute which cannot be set atomicallySecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
createDirectories
Creates a directory and all nonexistent parent directories. Unlike thecreateDirectory(java.nio.file.attribute.FileAttribute<?>...)
theFileAlreadyExistsException
is not thrown if the directory already exists.- Parameters:
attributes
- the optional attributes to set atomically when creating the new file- Throws:
FileAlreadyExistsException
- if a file (not a directory) already exists on given pathIOException
- in case of IO errorUnsupportedOperationException
- if the attributes contain an attribute which cannot be set atomicallySecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
delete
Deletes the file. If theTruffleFile
denotes a directory, the directory must be empty before deleting. If theTruffleFile
denotes a symbolic link the symbolic link itself is deleted not its target.- Throws:
NoSuchFileException
- if the file does not existDirectoryNotEmptyException
- if theTruffleFile
denotes a non empty directoryIOException
- in case of IO errorSecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
move
Moves or renames the file.- Parameters:
target
- the path of a target fileoptions
- the options specifying how the move should be performed, seeStandardCopyOption
- Throws:
UnsupportedOperationException
- ifoptions
contains unsupported optionFileAlreadyExistsException
- if the target path already exists and theoptions
don't containStandardCopyOption.REPLACE_EXISTING
optionDirectoryNotEmptyException
- if theoptions
containStandardCopyOption.REPLACE_EXISTING
but thetarget
is a non empty directoryAtomicMoveNotSupportedException
- if theoptions
containStandardCopyOption.ATOMIC_MOVE
but file cannot be moved atomicallyIOException
- in case of IO errorSecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
getPosixPermissions
Returns the file's Posix permissions.- Parameters:
linkOptions
- the options determining how the symbolic links should be handled- Returns:
- the the file's Posix permissions
- Throws:
IOException
- in case of IO errorUnsupportedOperationException
- when the Posix permissions are not supported by filesystemSecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
setPosixPermissions
public void setPosixPermissions(Set<? extends PosixFilePermission> permissions, LinkOption... linkOptions) throws IOException Sets the file's Posix permissions.- Parameters:
permissions
- the Posix permissions to setlinkOptions
- the options determining how the symbolic links should be handled- Throws:
IOException
- in case of IO errorUnsupportedOperationException
- when the Posix permissions are not supported by filesystemSecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
toString
-
hashCode
public int hashCode() -
equals
-
normalize
Returns aTruffleFile
with removed redundant name elements in it's path.- Returns:
- the normalized
TruffleFile
- Since:
- 19.0
-
relativize
Creates aTruffleFile
with a relative path between thisTruffleFile
and a givenTruffleFile
.Relativization is the inverse of
resolution
. Relativization constructs aTruffleFile
with relative path that whenresolved
against thisTruffleFile
yields aTruffleFile
locating the same file as givenTruffleFile
. A relative path cannot be constructed if only one of theTruffleFile
s isabsolute
.- Parameters:
other
- theTruffleFile
to relativize against thisTruffleFile
- Returns:
- the
TruffleFile
with relative path between this andother
TruffleFile
s - Throws:
IllegalArgumentException
- whenother
cannot be relativized against thisTruffleFile
- Since:
- 19.0
-
startsWith
Tests if thisTruffleFile
path starts with the given path. The pathfoo/bar
starts withfoo
andfoo/bar
but does not start withf
.- Parameters:
other
- the path- Returns:
true
if thisTruffleFile
path starts with given path- Throws:
IllegalArgumentException
- if the path cannot be parsed.- Since:
- 19.0
-
startsWith
Tests if thisTruffleFile
path starts with the givenTruffleFile
path. The pathfoo/bar
starts withfoo
andfoo/bar
but does not start withf
.- Parameters:
other
- theTruffleFile
- Returns:
true
if thisTruffleFile
path starts with givenTruffleFile
path- Since:
- 19.0
-
endsWith
Tests if thisTruffleFile
path ends with the given path. The pathfoo/bar
ends withbar
andfoo/bar
but does not end withr
.- Parameters:
other
- the path- Returns:
true
if thisTruffleFile
path ends with given path- Throws:
IllegalArgumentException
- if the path cannot be parsed.- Since:
- 19.0
-
endsWith
Tests if thisTruffleFile
path ends with the givenTruffleFile
path. The pathfoo/bar
ends withbar
andfoo/bar
but does not end withr
.- Parameters:
other
- theTruffleFile
- Returns:
true
if thisTruffleFile
path ends with givenTruffleFile
path- Since:
- 19.0
-
createLink
Creates a new link to an existing target (optional operation).- Parameters:
target
- the existing file to link- Throws:
FileAlreadyExistsException
- if the file or directory already exists on given pathIOException
- in case of IO errorUnsupportedOperationException
- if theFileSystem
implementation does not support linksSecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
createSymbolicLink
Creates a symbolic link to a target (optional operation).- Parameters:
target
- the target of the symbolic linkattrs
- the optional attributes to set atomically when creating the symbolic link- Throws:
FileAlreadyExistsException
- if the file or directory already exists on given pathIOException
- in case of IO errorUnsupportedOperationException
- if theFileSystem
implementation does not support symbolic links or the attributes contain an attribute which cannot be set atomicallySecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
readSymbolicLink
Reads the target of a symbolic link.- Returns:
- the
TruffleFile
representing the target of the symbolic link - Throws:
NotLinkException
- if theTruffleFile
is not a symbolic linkIOException
- in case of IO errorUnsupportedOperationException
- if theFileSystem
implementation does not support symbolic linksSecurityException
- if theFileSystem
denied the operation- Since:
- 20.3
-
getOwner
Returns the owner of the file.- Parameters:
options
- the options determining how the symbolic links should be handled- Returns:
- the file owner
- Throws:
IOException
- in case of IO errorUnsupportedOperationException
- if theFileSystem
implementation does not support owner attributeSecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
getGroup
Returns the group owner of the file.- Parameters:
options
- the options determining how the symbolic links should be handled- Returns:
- the file owner
- Throws:
IOException
- in case of IO errorUnsupportedOperationException
- if theFileSystem
implementation does not support group owner attributeSecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
newDirectoryStream
Opens a directory, returning aDirectoryStream
to iterate over all entries in the directory.The
TruffleFile
s returned by the directory stream'siterator
are created as if byresolving
the name of the directory entry against thisTruffleFile
.In most cases, the returned
DirectoryStream
should be closed using try-with-resources construct. When the stream must keep being opened for the lifetime of a context it should beregistered
for automatic close on context dispose. When not using the try-with-resources construct, then the directory stream'sclose
method should be called after iteration is completed.The code which iterates over all files can use simpler
list()
method.- Returns:
- a new opened
DirectoryStream
object - Throws:
IOException
- in case of IO errorSecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
visit
public void visit(FileVisitor<TruffleFile> visitor, int maxDepth, FileVisitOption... options) throws IOException Visits thisTruffleFile
file tree.This method walks a file tree rooted at this
TruffleFile
. The file tree traversal is depth-first. The appropriate method on giveFileVisitor
is invoked for each met file. File tree traversal completes when all accessible files in the tree have been visited, aFileVisitor
visit method returns aFileVisitResult.TERMINATE
or aFileVisitor
method terminates due to an uncaught exception.For each file encountered this method attempts to read its
BasicFileAttributes
. If the file is not a directory then thevisitFile
method is invoked with the file attributes. If the file attributes cannot be read, due to an I/O exception, then thevisitFileFailed
method is invoked with the I/O exception.Where the file is a directory, and the directory could not be opened, then the
visitFileFailed
method is invoked with the I/O exception, after which, the file tree walk continues, by default, at the next sibling of the directory.Where the directory is opened successfully, then the entries in the directory, and their descendants are visited. When all entries have been visited, or an I/O error occurs during iteration of the directory, then the directory is closed and the visitor's
postVisitDirectory
method is invoked. The file tree walk then continues, by default, at the next sibling of the directory.By default, symbolic links are not automatically followed by this method. If the
options
parameter contains theFOLLOW_LINKS
option then symbolic links are followed.The
maxDepth
parameter is the maximum number of levels of directories to visit. A value of0
means that only the starting file is visited. ThevisitFile
method is invoked for all files, including directories, encountered atmaxDepth
, unless the basic file attributes cannot be read, in which case thevisitFileFailed
method is invoked.- Parameters:
visitor
- theFileVisitor
to invoke for each filemaxDepth
- the maximum number of directory levels to visit,MAX_VALUE
may be used to indicate that all levels should be visited.options
- the options configuring the file tree traversal- Throws:
IllegalArgumentException
- if themaxDepth
parameter is negativeIOException
- in case of IO errorSecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
copy
Copies the file. When the file is a directory the copy creates an empty directory in the target location, the directory entries are not copied. This method can be used with thevisit
method to copy the whole sub-tree.- Parameters:
target
- the path of a target fileoptions
- the options specifying how the copy should be performed, seeStandardCopyOption
- Throws:
UnsupportedOperationException
- ifoptions
contains unsupported optionFileAlreadyExistsException
- if the target path already exists and theoptions
don't containStandardCopyOption.REPLACE_EXISTING
optionDirectoryNotEmptyException
- if theoptions
containStandardCopyOption.REPLACE_EXISTING
but thetarget
is a non empty directoryIOException
- in case of IO errorSecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
getMimeType
Deprecated.usedetectMimeType()
Returns thefile
MIME type.- Returns:
- the MIME type or
null
if the MIME type is not recognized - Throws:
IOException
- in case of IO errorSecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
detectMimeType
Detects thefile
MIME type.- Returns:
- the MIME type or
null
if the MIME type is not recognized - Throws:
SecurityException
- if theFileSystem
denied the operation- Since:
- 22.2
-
isSameFile
Tests if this and the givenTruffleFile
refer to the same physical file. If bothTruffleFile
objects areequal
then this method returnstrue
without any checks. If theTruffleFile
s have different filesystems then this method returnsfalse
. Otherwise, this method checks if bothTruffleFile
s refer to the same physical file. Depending on theFileSystem
implementation it may require to read the files attributes. This implies:- Public and Internal files with disabled IO are never the same.
- Public and Internal files with allowed IO are potentially the same.
- Files created by different languages are potentially the same.
- Files created during the Context pre-initialization and files created during Context execution are potentially the same.
- Parameters:
other
- the otherTruffleFile
- Returns:
true
if this and the givenTruffleFile
refer to the same physical file- Throws:
IOException
- in case of IO errorSecurityException
- if theFileSystem
denied the operation- Since:
- 20.2.0
-
getAttribute
public <T> T getAttribute(TruffleFile.AttributeDescriptor<T> attribute, LinkOption... linkOptions) throws IOException Reads a single file's attribute.- Parameters:
attribute
- the attribute to readlinkOptions
- the options determining how the symbolic links should be handled- Returns:
- the attribute value
- Throws:
IOException
- in case of IO errorUnsupportedOperationException
- when the filesystem does not support required attribute.SecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
setAttribute
public <T> void setAttribute(TruffleFile.AttributeDescriptor<T> attribute, T value, LinkOption... linkOptions) throws IOException Sets a single file's attribute.- Parameters:
attribute
- the attribute to setvalue
- the attribute valuelinkOptions
- the options determining how the symbolic links should be handled- Throws:
IOException
- in case of IO errorUnsupportedOperationException
- when the filesystem does not support given attributeIllegalArgumentException
- when the attribute value has an inappropriate valueSecurityException
- if theFileSystem
denied the operation- Since:
- 19.1.0
-
getAttributes
public TruffleFile.Attributes getAttributes(Collection<? extends TruffleFile.AttributeDescriptor<?>> attributes, LinkOption... linkOptions) throws IOException Reads file's attributes as a bulk operation.- Parameters:
attributes
- the attributes to readlinkOptions
- the options determining how the symbolic links should be handled- Returns:
- the
attributes view
- Throws:
IllegalArgumentException
- when no attributes are givenIOException
- in case of IO errorUnsupportedOperationException
- when the filesystem does not support some of the required attributes.SecurityException
- if theFileSystem
denied the operation- Since:
- 19.0
-
detectMimeType()