Interface FileObject
-
- All Superinterfaces:
java.lang.AutoCloseable,java.io.Closeable,java.lang.Comparable<FileObject>,java.lang.Iterable<FileObject>
- All Known Implementing Classes:
AbstractFileObject,Bzip2FileObject,CompressedFileFileObject,DecoratedFileObject,DelegateFileObject,FtpFileObject,GzipFileObject,Http4FileObject,HttpFileObject,JarFileObject,LocalFile,OnCallRefreshFileObject,RamFileObject,SftpFileObject,SynchronizedFileObject,TarFileObject,UrlFileObject,ZipFileObject
public interface FileObject extends java.lang.Comparable<FileObject>, java.lang.Iterable<FileObject>, java.io.Closeable
Represents a file, and is used to access the content and structure of the file.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.
File Naming
TODO - write this.
Reading and Writing a File
Reading and writing a file, and all other operations on the file's content, is done using the
FileContentobject returned bygetContent().Creating and Deleting a File
A file is created using either
createFolder(),createFile(), or by writing to the file using one of theFileContentmethods.A file is deleted using
delete(). Recursive deletion can be done usingdelete(FileSelector).Finding Files
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 the same file system.getChildren()andgetChild(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.Iterating Files
You can iterate over a FileObject using the Java "foreach" statement, which provides all descendants of a File Object.
Sorting Files
Files may be sorted using
Arrays.sort()andList.sort().- See Also:
FileSystemManager,FileContent,FileName
-
-
Field Summary
Fields Modifier and Type Field Description static FileObject[]EMPTY_ARRAYAn empty immutableFileObjectarray.
-
Method Summary
All Methods Instance Methods Abstract Methods Default Methods Modifier and Type Method Description booleancanRenameTo(FileObject newfile)Queries the file if it is possible to rename it to newfile.voidclose()Closes this file, and its content.voidcopyFrom(FileObject srcFile, FileSelector selector)Copies another file, and all its descendants, to this file.voidcreateFile()Creates this file, if it does not exist.voidcreateFolder()Creates this folder, if it does not exist.booleandelete()Deletes this file.intdelete(FileSelector selector)Deletes all descendants of this file that match a selector.intdeleteAll()Deletes this file and all children.booleanexists()Determines if this file exists.FileObject[]findFiles(FileSelector selector)Finds the set of matching descendants of this file, in depthwise order.voidfindFiles(FileSelector selector, boolean depthwise, java.util.List<FileObject> selected)Finds the set of matching descendants of this file.FileObjectgetChild(java.lang.String name)Gets a child of this file.FileObject[]getChildren()Gets a lists of children of this file.FileContentgetContent()Gets this file's content.FileOperationsgetFileOperations()Gets this instance's FileOperations.FileSystemgetFileSystem()Gets the file system that contains this file.FileNamegetName()Gets the name of this file.FileObjectgetParent()Gets the folder that contains this file.default java.nio.file.PathgetPath()Gets a Path representing this file.java.lang.StringgetPublicURIString()Gets the receiver as a URI String for public display, like, without a password.FileTypegetType()Gets this file's type.default java.net.URIgetURI()Gets a URI representing this file.java.net.URLgetURL()Gets a URL representing this file.booleanisAttached()Checks if the fileObject is attached.booleanisContentOpen()Checks if someone reads/write to this file.booleanisExecutable()Determines if this file is executable.booleanisFile()Checks if this file is a regular file.booleanisFolder()Checks if this file is a folder.booleanisHidden()Determines if this file is hidden.booleanisReadable()Determines if this file can be read.default booleanisSymbolicLink()Determines if this file is a symbolic link.booleanisWriteable()Determines if this file can be written to.voidmoveTo(FileObject destFile)Move this file.voidrefresh()This will prepare the fileObject to get resynchronized with the underlying file system if required.FileObjectresolveFile(java.lang.String path)Finds a file, relative to this file.FileObjectresolveFile(java.lang.String name, NameScope scope)Finds a file relative to this file.booleansetExecutable(boolean executable, boolean ownerOnly)Sets the owner's (or everybody's) write permission.booleansetReadable(boolean readable, boolean ownerOnly)Sets the owner's (or everybody's) read permission.booleansetWritable(boolean writable, boolean ownerOnly)Sets the owner's (or everybody's) write permission.
-
-
-
Field Detail
-
EMPTY_ARRAY
static final FileObject[] EMPTY_ARRAY
An empty immutableFileObjectarray.- Since:
- 2.8.0
-
-
Method Detail
-
canRenameTo
boolean canRenameTo(FileObject newfile)
Queries the file if it is possible to rename it to newfile.- Parameters:
newfile- the new file(-name)- Returns:
- true it this is the case
-
close
void close() throws FileSystemException
Closes this file, and its content. This method is a hint to the implementation that it can release any resources associated with the file.The file object can continue to be used after this method is called.
- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.io.Closeable- Throws:
FileSystemException- On error closing the file.- See Also:
FileContent.close()
-
copyFrom
void copyFrom(FileObject srcFile, FileSelector selector) throws FileSystemException
Copies another file, and all its descendants, to this file.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.
- Parameters:
srcFile- The source file to copy.selector- The selector to use to select which files to copy.- Throws:
FileSystemException- If this file is read-only, or if the source file does not exist, or on error copying the file.
-
createFile
void createFile() throws FileSystemException
Creates this file, if it does not exist. Also creates any ancestor folders which do not exist. This method does nothing if the file already exists and is a file.- Throws:
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.
-
createFolder
void createFolder() throws FileSystemException
Creates this folder, if it does not exist. Also creates any ancestor folders which do not exist. This method does nothing if the folder already exists.- Throws:
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.
-
delete
boolean delete() throws FileSystemException
Deletes this file. Does nothing if this file does not exist of if it is a folder that has children. Does not delete any descendants of this file, usedelete(FileSelector)ordeleteAll()for that.- Returns:
- true if this object has been deleted
- Throws:
FileSystemException- If this file is a non-empty folder, or if this file is read-only, or on error deleteing this file.
-
delete
int delete(FileSelector selector) throws FileSystemException
Deletes all descendants of this file that match a selector. Does nothing if this file does not exist.This method is not transactional. If it fails and throws an exception, this file will potentially only be partially deleted.
- Parameters:
selector- The selector to use to select which files to delete.- Returns:
- the number of deleted objects
- Throws:
FileSystemException- If this file or one of its descendants is read-only, or on error deleting this file or one of its descendants.
-
deleteAll
int deleteAll() throws FileSystemException
Deletes this file and all children.- Returns:
- the number of deleted files.
- Throws:
FileSystemException- if an error occurs.- See Also:
delete(FileSelector),Selectors.SELECT_ALL
-
exists
boolean exists() throws FileSystemException
Determines if this file exists.- Returns:
trueif this file exists,falseif not.- Throws:
FileSystemException- On error determining if this file exists.
-
findFiles
FileObject[] findFiles(FileSelector selector) throws FileSystemException
Finds the set of matching descendants of this file, in depthwise order.- Parameters:
selector- The selector to use to select matching files.- Returns:
- The matching files. The files are returned in depthwise order (that is, a child appears in the list before its parent).
- Throws:
FileSystemException- if an error occurs.
-
findFiles
void findFiles(FileSelector selector, boolean depthwise, java.util.List<FileObject> selected) throws FileSystemException
Finds the set of matching descendants of this file.- Parameters:
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.- Throws:
FileSystemException- if an error occurs.
-
getChild
FileObject getChild(java.lang.String name) throws FileSystemException
Gets a child of this file. Note that this method returnsnullwhen the child does not exist. This differs fromresolveFile(String, NameScope)which never returns null.- Parameters:
name- The name of the child.- Returns:
- The child, or null if there is no such child.
- Throws:
FileSystemException- If this file does not exist, or is not a folder, or on error determining this file's children.
-
getChildren
FileObject[] getChildren() throws FileSystemException
Gets a lists of children of this file.- Returns:
- An array containing the children of this file. The array is unordered. If the file does not have any children, a zero-length array is returned. This method never returns null.
- Throws:
FileSystemException- If this file does not exist, or is not a folder, or on error listing this file's children.
-
getContent
FileContent getContent() throws FileSystemException
Gets this file's content. TheFileContentreturned 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
FileContentcan be used to create the file by writing its content.- Returns:
- This file's content.
- Throws:
FileSystemException- On error getting this file's content.
-
getFileOperations
FileOperations getFileOperations() throws FileSystemException
Gets this instance's FileOperations.- Returns:
- FileOperations interface that provides access to the operations API.
- Throws:
FileSystemException- if an error occurs.
-
getFileSystem
FileSystem getFileSystem()
Gets the file system that contains this file.- Returns:
- The file system.
-
getParent
FileObject getParent() throws FileSystemException
Gets the folder that contains this file.- Returns:
- The folder that contains this file. Returns null if this file is the root of a file system.
- Throws:
FileSystemException- On error finding the file's parent.
-
getPath
default java.nio.file.Path getPath()
Gets a Path representing this file.- Returns:
- the Path for the file.
- Since:
- 2.7.0
-
getPublicURIString
java.lang.String getPublicURIString()
Gets the receiver as a URI String for public display, like, without a password.- Returns:
- A URI String without a password, never
null.
-
getType
FileType getType() throws FileSystemException
Gets this file's type.- Returns:
- One of the
FileTypeconstants. Never returns null. - Throws:
FileSystemException- On error determining the file's type.
-
getURI
default java.net.URI getURI()
Gets a URI representing this file.- Returns:
- the URI for the file.
- Since:
- 2.7.0
-
getURL
java.net.URL getURL() throws FileSystemException
Gets a URL representing this file.- Returns:
- the URL for the file.
- Throws:
FileSystemException- if an error occurs.
-
isAttached
boolean isAttached()
Checks if the fileObject is attached.- Returns:
- true if the FileObject is attached.
-
isContentOpen
boolean isContentOpen()
Checks if someone reads/write to this file.- Returns:
- true if the file content is open.
-
isExecutable
boolean isExecutable() throws FileSystemException
Determines if this file is executable.- Returns:
trueif this file is executable,falseif not.- Throws:
FileSystemException- On error determining if this file exists.
-
isFile
boolean isFile() throws FileSystemException
Checks if this file is a regular file.- Returns:
- true if this file is a regular file.
- Throws:
FileSystemException- if an error occurs.- Since:
- 2.1
- See Also:
getType(),FileType.FILE
-
isFolder
boolean isFolder() throws FileSystemException
Checks if this file is a folder.- Returns:
- true if this file is a folder.
- Throws:
FileSystemException- if an error occurs.- Since:
- 2.1
- See Also:
getType(),FileType.FOLDER
-
isHidden
boolean isHidden() throws FileSystemException
Determines if this file is hidden.- Returns:
trueif this file is hidden,falseif not.- Throws:
FileSystemException- On error determining if this file exists.
-
isReadable
boolean isReadable() throws FileSystemException
Determines if this file can be read.- Returns:
trueif this file is readable,falseif not.- Throws:
FileSystemException- On error determining if this file exists.
-
isSymbolicLink
default boolean isSymbolicLink() throws FileSystemException
Determines if this file is a symbolic link.- Returns:
trueif this file is a symbolic link,falseif not.- Throws:
FileSystemException- On error determining if this file exists.- Since:
- 2.4
-
isWriteable
boolean isWriteable() throws FileSystemException
Determines if this file can be written to.- Returns:
trueif this file is writable,falseif not.- Throws:
FileSystemException- On error determining if this file exists.
-
moveTo
void moveTo(FileObject destFile) throws FileSystemException
Move this file.If the destFile exists, it is deleted first.
- Parameters:
destFile- the New file name.- Throws:
FileSystemException- If this file is read-only, or if the source file does not exist, or on error copying the file.
-
refresh
void refresh() throws FileSystemException
This will prepare the fileObject to get resynchronized with the underlying file system if required.- Throws:
FileSystemException- if an error occurs.
-
resolveFile
FileObject resolveFile(java.lang.String path) throws FileSystemException
Finds a file, relative to this file. Equivalent to callingresolveFile( path, NameScope.FILE_SYSTEM ).- Parameters:
path- The path of the file to locate. Can either be a relative path or an absolute path.- Returns:
- The file.
- Throws:
FileSystemException- On error parsing the path, or on error finding the file.
-
resolveFile
FileObject resolveFile(java.lang.String name, NameScope scope) throws FileSystemException
Finds a file relative to this file. Refer toNameScopefor a description of how names are resolved in the different scopes.- Parameters:
name- The name to resolve.scope- the NameScope for the file.- Returns:
- The file.
- Throws:
FileSystemException- On error parsing the path, or on error finding the file.
-
setExecutable
boolean setExecutable(boolean executable, boolean ownerOnly) throws FileSystemException
Sets the owner's (or everybody's) write permission.- Parameters:
executable- True to allow read access, false to disallow.ownerOnly- Iftrue, the permission applies only to the owner; otherwise, it applies to everybody.- Returns:
- true if the operation succeeded.
- Throws:
FileSystemException- On error determining if this file exists.- Since:
- 2.1
-
setReadable
boolean setReadable(boolean readable, boolean ownerOnly) throws FileSystemException
Sets the owner's (or everybody's) read permission.- Parameters:
readable- True to allow read access, false to disallowownerOnly- Iftrue, the permission applies only to the owner; otherwise, it applies to everybody.- Returns:
- true if the operation succeeded
- Throws:
FileSystemException- On error determining if this file exists.- Since:
- 2.1
-
setWritable
boolean setWritable(boolean writable, boolean ownerOnly) throws FileSystemException
Sets the owner's (or everybody's) write permission.- Parameters:
writable- True to allow read access, false to disallowownerOnly- Iftrue, the permission applies only to the owner; otherwise, it applies to everybody.- Returns:
- true if the operation succeeded
- Throws:
FileSystemException- On error determining if this file exists.- Since:
- 2.1
-
-