java.lang.Object
org.bzdev.io.DirectoryAccessor
Class to provide access to a directory.
The methods allow access to files and directories based on the
permissions specified when this class's constructor is called.
-
Constructor Summary
ConstructorsConstructorDescriptionDirectoryAccessor
(File dir) Constructor given a File.DirectoryAccessor
(File dir, boolean readOnly) Constructor given a File and mode.DirectoryAccessor
(String dirname) Constructor given a directory name.DirectoryAccessor
(String dirname, boolean readOnly) Constructor given a directory name and mode. -
Method Summary
Modifier and TypeMethodDescriptionaddDirectory
(File dir) Add a new directory, specified by a relative File.addDirectory
(String dir) Add a new directory, specified by a relative path name.boolean
Determine if this directory accessor allows files to be read or existing subdirectories to be accessed with a directory accessor that allows reading.boolean
Determine if this directory accessor allows files in its directory to be written or new subdirectories to be created with that property.boolean
Determine if a file in a directory provided by a directory accessor is readable.boolean
Determine if a file in a directory provided by a directory accessor is writable.void
copy
(String f1, String f2, CopyOption... options) Copy a file from the directory associated with this directory accessor to another location in the same directory.void
copy
(String f1, DirectoryAccessor da, String f2, CopyOption... options) Copy a file from the directory associated with this directory accessor to a location in a possibly different directory.createFileAccessor
(File file) Create a new FileAccessor given a File for this instance's directory.createFileAccessor
(File file, String mode) Create a new FileAccessor given a File for this instance's directory and given a file-accessor mode.createFileAccessor
(String file) Create a new FileAccessor for a file in this instance's directory.createFileAccessor
(String file, String mode) Create a new FileAccessor for a file in this instance's directory given a file-accessor mode.boolean
Delete a file or directory.boolean
Determine if a file name refers to a file or directory that exists The name is the filename component, not the full path.getInputStream
(File file) Get an input stream given a File.getInputStream
(String name) Get an input stream given a file name.getOutputStream
(File file) Get an output stream given a File.getOutputStream
(String name) Get an output stream given a file name.getRandomAccessFile
(File file, String mode) Get a RandomAccessFile for a file.getRandomAccessFile
(String filename, String mode) Get a RandomAccessFile for a file specified by its name.boolean
isDirectory
(String filename) Determine if a file name refers to a directory The name is the filename component, not the full path.String[]
list()
Get a list of the names of files and subdirectories in the directory associated with this directory accessor.Generate a list of file accessors for the files in the directory corresponding to this directory accesssor.void
move
(String f1, String f2, CopyOption... options) Move the file from the directory associated with this directory accessor to another location in the same directory.void
move
(String f1, DirectoryAccessor da, String f2, CopyOption... options) Move the file from the directory associated with this directory accessor to another location in a possibly differnt directory.
-
Constructor Details
-
DirectoryAccessor
Constructor given a directory name.- Parameters:
dirname
- the directory name- Throws:
IOException
- an IO error occurred, probably due to permissions or a missing directory component.
-
DirectoryAccessor
Constructor given a File.- Parameters:
dir
- the directory- Throws:
IOException
- an IO error occurred, probably due to permissions or a missing directory component.
-
DirectoryAccessor
Constructor given a directory name and mode.- Parameters:
dirname
- the directory namereadOnly
- true if the directory accessor does not permit new files to be added to the directory or existing files to be written- Throws:
IOException
- an IO error occurred, probably due to permissions or a missing directory component.
-
DirectoryAccessor
Constructor given a File and mode.- Parameters:
dir
- the directoryreadOnly
- true if the directory accessor does not permit new files to be added to the directory or existing files to be written- Throws:
IOException
- an IO error occurred, probably due to permissions or a missing directory component.
-
-
Method Details
-
allowsReading
public boolean allowsReading()Determine if this directory accessor allows files to be read or existing subdirectories to be accessed with a directory accessor that allows reading.- Returns:
- true if the directory accessor allows files to be written or created; false otherwise
-
allowsWriting
public boolean allowsWriting()Determine if this directory accessor allows files in its directory to be written or new subdirectories to be created with that property.Note: a directory accessor will not allow writing if it was created in read-only mode.
- Returns:
- true if the directory accessor allows files to be written or created; false otherwise
-
createFileAccessor
Create a new FileAccessor for a file in this instance's directory. The file must be specified by a relative path name or relative file and may not include directory components.- Parameters:
file
- the name of the file- Returns:
- the new file accessor
- Throws:
IOException
- an IO error occurred, probably due to permissions or a missing directory component.
-
createFileAccessor
Create a new FileAccessor for a file in this instance's directory given a file-accessor mode. The file must be specified by a relative path name or relative file and may not include directory components.- Parameters:
file
- the name of the filemode
- the file-accessor mode (a combination of "r", "w", and "a" as specified by constructors forFileAccessor
- Returns:
- the new file accessor
- Throws:
IOException
- an IO error occurred, probably due to permissions or a missing directory component.
-
createFileAccessor
Create a new FileAccessor given a File for this instance's directory. The file must be specified by a relative path name or relative file and may not include directory components.- Parameters:
file
- the file to access- Returns:
- the new file accessor
- Throws:
IOException
- an IO error occurred, probably due to permissions or a missing directory component.
-
createFileAccessor
public FileAccessor createFileAccessor(File file, String mode) throws IOException, IllegalArgumentException Create a new FileAccessor given a File for this instance's directory and given a file-accessor mode. The file must be specified by a relative path name or relative file and may not include directory components.- Parameters:
file
- the file to accessmode
- the requested file-accessor mode (a combination of "r", "w", and "a" as specified by constructors forFileAccessor
- Returns:
- the new file accessor
- Throws:
IOException
- an IO error occurred, probably due to permissions or a missing directory component.IllegalArgumentException
- the specified file has a directory component or is not absolute, or the mode included a "w" or "a" character when this directory accessor is read-only
-
getInputStream
Get an input stream given a file name.- Parameters:
name
- the name of the file for which an input stream will be opened- Returns:
- the input stream
- Throws:
IOException
- an IO error occurred
-
getInputStream
Get an input stream given a File.- Parameters:
file
- the file for which an input stream will be opened- Returns:
- the input stream
- Throws:
IOException
- an IO error occurred
-
getOutputStream
Get an output stream given a file name. The file will be created if it does not already exist.- Parameters:
name
- the name of the file for which an output stream will be opened- Returns:
- the output stream
- Throws:
IOException
- an IO error occurred
-
getOutputStream
Get an output stream given a File. The file will be created if it does not already exist.- Parameters:
file
- the file for which an output stream will be opened- Returns:
- the output stream
- Throws:
IOException
- an IO error occurred, probably due to permissions or a missing directory component.
-
getRandomAccessFile
Get a RandomAccessFile for a file specified by its name.- Parameters:
filename
- the file namemode
- the mode ("r" or "w") defined byFileAccessor
- Returns:
- the random-access file
- Throws:
IOException
- the file could not be opened, possibly because a "r" or "a" character in the mode was used with a read-only directory accessor
-
getRandomAccessFile
Get a RandomAccessFile for a file.- Parameters:
file
- the filemode
- the mode ("r" or "w") defined byFileAccessor
- Returns:
- the random-access file
- Throws:
IOException
- the file could not be opened, possibly because a "r" or "a" character in the mode was used with a read-only directory accessor
-
addDirectory
public DirectoryAccessor addDirectory(String dir) throws IOException, IllegalArgumentException, RuntimeException, NullPointerException Add a new directory, specified by a relative path name. The directory must be specified by a relative path name or relative file and may not include multiple directory components.- Parameters:
dir
- the name of the directory to create- Returns:
- a new directory accessor
- Throws:
IOException
- an IO error occurred, probably due to permissions or a missing directory component.IllegalArgumentException
RuntimeException
NullPointerException
-
addDirectory
public DirectoryAccessor addDirectory(File dir) throws IOException, RuntimeException, IllegalArgumentException, NullPointerException, IllegalStateException Add a new directory, specified by a relative File. The directory must be specified by a relative path name or relative file and may not include multiple directory components. If the directory already exists, a directory accessor for the existing directory will be returned. This directory accessor must be writeable or an exception will be thrown.- Parameters:
dir
- the directory to create- Returns:
- a new directory accessor
- Throws:
IOException
- an IO error occurred, probably due to permissions or a missing directory component.IllegalStateException
- this directory accessor does not allow directories to be addedIllegalArgumentException
- the argument is a file with an absolute path name or has a parent directoryNullPointerException
- the argument was nullRuntimeException
-
list
Get a list of the names of files and subdirectories in the directory associated with this directory accessor.- Returns:
- the names of the files and directories
-
listFileAccessors
Generate a list of file accessors for the files in the directory corresponding to this directory accesssor. The file accessors included are ones for files that are readable when this directory accessor allows reading, and for files that are writeable when this directory allows writing.- Returns:
- the file accessors
- Throws:
IOException
- the directory corresponding to this directory accessor does not exist or is not readable- See Also:
-
canRead
Determine if a file in a directory provided by a directory accessor is readable. The name is the filename component, not the full path. If this directory accessor does not allow files to be read, this method will return false (unless an exception is thrown).- Parameters:
filename
- the name of a file or directory- Returns:
- true if the file or directory is readable; false otherwise
- Throws:
IOException
- an IO error occurred, probably due to permissions or a missing directory component.
-
canWrite
Determine if a file in a directory provided by a directory accessor is writable. The name is the filename component, not the full path. If this directory accessor does not allow files to be written, this method will return false (unless an exception is thrown).- Parameters:
filename
- the name of a file or directory- Returns:
- true if the file or directory is writable; false otherwise
- Throws:
IOException
- an IO error occurred, probably due to permissions or a missing directory component.
-
isDirectory
Determine if a file name refers to a directory The name is the filename component, not the full path.- Parameters:
filename
- the name of a directory or file name- Returns:
- true if the name refers to a directory; false otherwise
- Throws:
IOException
- an IO error occurred, probably due to permissions or a missing directory component.
-
exists
Determine if a file name refers to a file or directory that exists The name is the filename component, not the full path.- Parameters:
filename
- the name of a directory or file name- Returns:
- true if the name refers an existing file or directory; false otherwise
-
delete
Delete a file or directory. The name is the filename component, not the full path. If this directory accessor does not allow files to be written, this method will return false (unless an exception is thrown).- Parameters:
filename
- the name of a file or directory- Returns:
- true if the file or directory was deleted; false otherwise
- Throws:
IllegalStateException
- this directory accessor does not allow files to be deleted
-
move
public void move(String f1, String f2, CopyOption... options) throws IOException, FileAlreadyExistsException, DirectoryNotEmptyException, UnsupportedOperationException, IllegalStateException Move the file from the directory associated with this directory accessor to another location in the same directory. by another file accessor. File names must not contain directory components.- Parameters:
f1
- the name of the file to movef2
- the name of the new location.options
- copy options, each of which is eitherStandardCopyOption.ATOMIC_MOVE
orStandardCopyOption.REPLACE_EXISTING
- Throws:
UnsupportedOperationException
- if an option is not recognizedIOException
- an IO error occurred, possibly due to permissions or a missing directory componentFileAlreadyExistsException
- if the target file exists but cannot be replaced becuase the REPLACE_EXISTING option was not specifiedDirectoryNotEmptyException
- if the target file exists but cannot be replaced because it is a non-empty directoryIllegalStateException
- See Also:
-
copy
public void copy(String f1, String f2, CopyOption... options) throws IOException, FileAlreadyExistsException, DirectoryNotEmptyException, UnsupportedOperationException, IllegalStateException Copy a file from the directory associated with this directory accessor to another location in the same directory. File names must not contain directory components.- Parameters:
f1
- the file to copyf2
- the name of the target file.options
- copy options, each of which is eitherLinkOption.NOFOLLOW_LINKS
StandardCopyOption.COPY_ATTRIBUTES
orStandardCopyOption.REPLACE_EXISTING
- Throws:
UnsupportedOperationException
- if an option is not recognizedIOException
- an IO error occurred, possibly due to permissions or a missing directory componentFileAlreadyExistsException
- if the target file exists but cannot be replaced becuase the REPLACE_EXISTING option was not specifiedDirectoryNotEmptyException
- if the target file exists but cannot be replaced because it is a non-empty directoryIllegalStateException
- See Also:
-
move
public void move(String f1, DirectoryAccessor da, String f2, CopyOption... options) throws IOException, UnsupportedOperationException, IllegalStateException Move the file from the directory associated with this directory accessor to another location in a possibly differnt directory. by another file accessor. File names must not contain directory components.- Parameters:
f1
- the name of the file to moveda
- the directory accessor for the target file's locationf2
- the file name for the new location.options
- copy options, each of which is eitherStandardCopyOption.ATOMIC_MOVE
orStandardCopyOption.REPLACE_EXISTING
- Throws:
UnsupportedOperationException
- if an option is not recognizedIOException
- an IO error occurred, possibly due to permissions or a missing directory componentFileAlreadyExistsException
- if the target file exists but cannot be replaced becuase the RPLACE_EXISTING option was not specifiedDirectoryNotEmptyException
- if the target file exists but cannot be replaced because it is a non-empty directoryIllegalStateException
- See Also:
-
copy
public void copy(String f1, DirectoryAccessor da, String f2, CopyOption... options) throws IOException, UnsupportedOperationException, IllegalStateException Copy a file from the directory associated with this directory accessor to a location in a possibly different directory. File names must not contain directory components.- Parameters:
f1
- the file to copyda
- the directory accessorf2
- the name of the target file.options
- copy options, each of which is eitherLinkOption.NOFOLLOW_LINKS
StandardCopyOption.COPY_ATTRIBUTES
orStandardCopyOption.REPLACE_EXISTING
- Throws:
UnsupportedOperationException
- if an option is not recognizedIOException
- an IO error occurred, possibly due to permissions or a missing directory componentFileAlreadyExistsException
- if the target file exists but cannot be replaced becuase the REPLACE_EXISTING option was not specifiedDirectoryNotEmptyException
- if the target file exists but cannot be replaced because it is a non-empty directoryIllegalStateException
- See Also:
-