public interface FileObject extends Comparable<FileObject>, Iterable<FileObject>, Closeable
Files are arranged in a hierarchy. Each hierarchy forms a file system. A file system represents things like a local OS file system, a windows share, an HTTP server, or the contents of a Zip file.
There are two types of files: Folders, which contain other files, and normal files, which contain data, or content. A folder may not have any content, and a normal file cannot contain other files.
TODO - write this.
Reading and writing a file, and all other operations on the file's content, is done using the
FileContent
object returned by getContent()
.
A file is created using either createFolder()
, createFile()
, or by writing to the file using one of the
FileContent
methods.
A file is deleted using delete()
. Recursive deletion can be done using delete(FileSelector)
.
Other files in the same file system as this file can be found using:
findFiles(org.apache.commons.vfs2.FileSelector)
to find a set of matching descendants in in the same file system.getChildren()
and getChild(java.lang.String)
to find the children of this file.getParent()
to find the folder containing this file.getFileSystem()
to find another file in the same file system.resolveFile(java.lang.String)
to find another file relative to this file.
To find files in another file system, use a FileSystemManager
.
You can iterate over a FileObject using the Java "foreach" statement, which provides all descendants of a File Object.
Files may be sorted using Arrays.sort()
and
Collections.sort()
.
FileSystemManager
,
FileContent
,
FileName
Modifier and Type | Field and Description |
---|---|
static FileObject[] |
EMPTY_ARRAY
An empty immutable
FileObject array. |
Modifier and Type | Method and Description |
---|---|
boolean |
canRenameTo(FileObject newfile)
Queries the file if it is possible to rename it to newfile.
|
void |
close()
Closes this file, and its content.
|
void |
copyFrom(FileObject srcFile,
FileSelector selector)
Copies another file, and all its descendants, to this file.
|
void |
createFile()
Creates this file, if it does not exist.
|
void |
createFolder()
Creates this folder, if it does not exist.
|
boolean |
delete()
Deletes this file.
|
int |
delete(FileSelector selector)
Deletes all descendants of this file that match a selector.
|
int |
deleteAll()
Deletes this file and all children.
|
boolean |
exists()
Determines if this file exists.
|
FileObject[] |
findFiles(FileSelector selector)
Finds the set of matching descendants of this file, in depthwise order.
|
void |
findFiles(FileSelector selector,
boolean depthwise,
List<FileObject> selected)
Finds the set of matching descendants of this file.
|
FileObject |
getChild(String name)
Returns a child of this file.
|
FileObject[] |
getChildren()
Lists the children of this file.
|
FileContent |
getContent()
Returns this file's content.
|
FileOperations |
getFileOperations() |
FileSystem |
getFileSystem()
Returns the file system that contains this file.
|
FileName |
getName()
Returns the name of this file.
|
FileObject |
getParent()
Returns the folder that contains this file.
|
default Path |
getPath()
Returns a Path representing this file.
|
String |
getPublicURIString()
Returns the receiver as a URI String for public display, like, without a password.
|
FileType |
getType()
Returns this file's type.
|
default URI |
getURI()
Returns a URI representing this file.
|
URL |
getURL()
Returns a URL representing this file.
|
boolean |
isAttached()
Checks if the fileObject is attached.
|
boolean |
isContentOpen()
Checks if someone reads/write to this file.
|
boolean |
isExecutable()
Determines if this file is executable.
|
boolean |
isFile()
Checks if this file is a regular file.
|
boolean |
isFolder()
Checks if this file is a folder.
|
boolean |
isHidden()
Determines if this file is hidden.
|
boolean |
isReadable()
Determines if this file can be read.
|
default boolean |
isSymbolicLink()
Determines if this file is a symbolic link.
|
boolean |
isWriteable()
Determines if this file can be written to.
|
void |
moveTo(FileObject destFile)
Move this file.
|
void |
refresh()
This will prepare the fileObject to get resynchronized with the underlying file system if required.
|
FileObject |
resolveFile(String path)
Finds a file, relative to this file.
|
FileObject |
resolveFile(String name,
NameScope scope)
Finds a file relative to this file.
|
boolean |
setExecutable(boolean executable,
boolean ownerOnly)
Sets the owner's (or everybody's) write permission.
|
boolean |
setReadable(boolean readable,
boolean ownerOnly)
Sets the owner's (or everybody's) read permission.
|
boolean |
setWritable(boolean writable,
boolean ownerOnly)
Sets the owner's (or everybody's) write permission.
|
compareTo
forEach, iterator, spliterator
static final FileObject[] EMPTY_ARRAY
FileObject
array.boolean canRenameTo(FileObject newfile)
newfile
- the new file(-name)void close() throws FileSystemException
The file object can continue to be used after this method is called.
close
in interface AutoCloseable
close
in interface Closeable
FileSystemException
- On error closing the file.FileContent.close()
void copyFrom(FileObject srcFile, FileSelector selector) throws FileSystemException
If this file does not exist, it is created. Its parent folder is also created, if necessary. If this file does exist, it is deleted first.
This method is not transactional. If it fails and throws an exception, this file will potentially only be partially copied.
srcFile
- The source file to copy.selector
- The selector to use to select which files to copy.FileSystemException
- If this file is read-only, or if the source file does not exist, or on error copying
the file.void createFile() throws FileSystemException
FileSystemException
- If the file already exists with the wrong type, or the parent folder is read-only, or
on error creating this file or one of its ancestors.void createFolder() throws FileSystemException
FileSystemException
- If the folder already exists with the wrong type, or the parent folder is read-only,
or on error creating this folder or one of its ancestors.boolean delete() throws FileSystemException
delete(FileSelector)
or deleteAll()
for that.FileSystemException
- If this file is a non-empty folder, or if this file is read-only, or on error
deleteing this file.int delete(FileSelector selector) throws FileSystemException
This method is not transactional. If it fails and throws an exception, this file will potentially only be partially deleted.
selector
- The selector to use to select which files to delete.FileSystemException
- If this file or one of its descendants is read-only, or on error deleting this file
or one of its descendants.int deleteAll() throws FileSystemException
FileSystemException
- if an error occurs.delete(FileSelector)
,
Selectors.SELECT_ALL
boolean exists() throws FileSystemException
true
if this file exists, false
if not.FileSystemException
- On error determining if this file exists.FileObject[] findFiles(FileSelector selector) throws FileSystemException
selector
- The selector to use to select matching files.FileSystemException
- if an error occurs.void findFiles(FileSelector selector, boolean depthwise, List<FileObject> selected) throws FileSystemException
selector
- the selector used to determine if the file should be selecteddepthwise
- controls the ordering in the list. e.g. deepest firstselected
- container for selected files. list needs not to be empty.FileSystemException
- if an error occurs.FileObject getChild(String name) throws FileSystemException
null
when the child does not exist. This
differs from resolveFile(String, NameScope)
which never returns null.name
- The name of the child.FileSystemException
- If this file does not exist, or is not a folder, or on error determining this file's
children.FileObject[] getChildren() throws FileSystemException
FileSystemException
- If this file does not exist, or is not a folder, or on error listing this file's
children.FileContent getContent() throws FileSystemException
FileContent
returned by this method can be used to read and write the
content of the file.
This method can be called if the file does not exist, and the returned FileContent
can be used to create
the file by writing its content.
FileSystemException
- On error getting this file's content.FileOperations getFileOperations() throws FileSystemException
FileSystemException
- if an error occurs.FileSystem getFileSystem()
FileName getName()
FileObject getParent() throws FileSystemException
FileSystemException
- On error finding the file's parent.String getPublicURIString()
null
.FileType getType() throws FileSystemException
FileType
constants. Never returns null.FileSystemException
- On error determining the file's type.default URI getURI()
default Path getPath()
URL getURL() throws FileSystemException
FileSystemException
- if an error occurs.boolean isAttached()
boolean isContentOpen()
boolean isExecutable() throws FileSystemException
true
if this file is executable, false
if not.FileSystemException
- On error determining if this file exists.boolean isFile() throws FileSystemException
FileSystemException
- if an error occurs.getType()
,
FileType.FILE
boolean isFolder() throws FileSystemException
FileSystemException
- if an error occurs.getType()
,
FileType.FOLDER
boolean isHidden() throws FileSystemException
true
if this file is hidden, false
if not.FileSystemException
- On error determining if this file exists.boolean isReadable() throws FileSystemException
true
if this file is readable, false
if not.FileSystemException
- On error determining if this file exists.default boolean isSymbolicLink() throws FileSystemException
true
if this file is a symbolic link, false
if not.FileSystemException
- On error determining if this file exists.boolean isWriteable() throws FileSystemException
true
if this file is writable, false
if not.FileSystemException
- On error determining if this file exists.void moveTo(FileObject destFile) throws FileSystemException
If the destFile exists, it is deleted first.
destFile
- the New file name.FileSystemException
- If this file is read-only, or if the source file does not exist, or on error copying
the file.void refresh() throws FileSystemException
FileSystemException
- if an error occurs.FileObject resolveFile(String path) throws FileSystemException
resolveFile( path, NameScope.FILE_SYSTEM )
.path
- The path of the file to locate. Can either be a relative path or an absolute path.FileSystemException
- On error parsing the path, or on error finding the file.FileObject resolveFile(String name, NameScope scope) throws FileSystemException
NameScope
for a description of how names are resolved in the different scopes.name
- The name to resolve.scope
- the NameScope for the file.FileSystemException
- On error parsing the path, or on error finding the file.boolean setExecutable(boolean executable, boolean ownerOnly) throws FileSystemException
executable
- True to allow read access, false to disallow.ownerOnly
- If true
, the permission applies only to the owner; otherwise, it applies to everybody.FileSystemException
- On error determining if this file exists.boolean setReadable(boolean readable, boolean ownerOnly) throws FileSystemException
readable
- True to allow read access, false to disallowownerOnly
- If true
, the permission applies only to the owner; otherwise, it applies to everybody.FileSystemException
- On error determining if this file exists.boolean setWritable(boolean writable, boolean ownerOnly) throws FileSystemException
writable
- True to allow read access, false to disallowownerOnly
- If true
, the permission applies only to the owner; otherwise, it applies to everybody.FileSystemException
- On error determining if this file exists.Copyright © 2002–2020 The Apache Software Foundation. All rights reserved.