Package org.bzdev.io

Class DirectoryAccessor

java.lang.Object
org.bzdev.io.DirectoryAccessor

public class DirectoryAccessor extends Object
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 Details

    • DirectoryAccessor

      public DirectoryAccessor(String dirname) throws IOException
      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

      public DirectoryAccessor(File dir) throws IOException
      Constructor given a File.
      Parameters:
      dir - the directory
      Throws:
      IOException - an IO error occurred, probably due to permissions or a missing directory component.
    • DirectoryAccessor

      public DirectoryAccessor(String dirname, boolean readOnly) throws IOException
      Constructor given a directory name and mode.
      Parameters:
      dirname - the directory name
      readOnly - 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

      public DirectoryAccessor(File dir, boolean readOnly) throws IOException
      Constructor given a File and mode.
      Parameters:
      dir - the directory
      readOnly - 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

      public FileAccessor createFileAccessor(String file) throws IOException
      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

      public FileAccessor createFileAccessor(String file, String mode) throws IOException
      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 file
      mode - the file-accessor mode (a combination of "r", "w", and "a" as specified by constructors for FileAccessor
      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) throws IOException
      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 access
      mode - the requested file-accessor mode (a combination of "r", "w", and "a" as specified by constructors for FileAccessor
      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

      public InputStream getInputStream(String name) throws IOException
      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

      public InputStream getInputStream(File file) throws IOException
      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

      public OutputStream getOutputStream(String name) throws IOException
      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

      public OutputStream getOutputStream(File file) throws IOException
      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

      public RandomAccessFile getRandomAccessFile(String filename, String mode) throws IOException
      Get a RandomAccessFile for a file specified by its name.
      Parameters:
      filename - the file name
      mode - the mode ("r" or "w") defined by FileAccessor
      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

      public RandomAccessFile getRandomAccessFile(File file, String mode) throws IOException
      Get a RandomAccessFile for a file.
      Parameters:
      file - the file
      mode - the mode ("r" or "w") defined by FileAccessor
      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

      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

      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 added
      IllegalArgumentException - the argument is a file with an absolute path name or has a parent directory
      NullPointerException - the argument was null
      RuntimeException
    • list

      public String[] 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

      public FileAccessor[] listFileAccessors() throws IOException
      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

      public boolean canRead(String filename) throws IOException
      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

      public boolean canWrite(String filename) throws IOException
      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

      public boolean isDirectory(String filename) throws IOException
      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

      public boolean exists(String filename)
      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

      public boolean delete(String filename) throws IllegalStateException
      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

      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 move
      f2 - the name of the new location.
      options - copy options, each of which is either StandardCopyOption.ATOMIC_MOVE or StandardCopyOption.REPLACE_EXISTING
      Throws:
      UnsupportedOperationException - if an option is not recognized
      IOException - an IO error occurred, possibly due to permissions or a missing directory component
      FileAlreadyExistsException - if the target file exists but cannot be replaced becuase the REPLACE_EXISTING option was not specified
      DirectoryNotEmptyException - if the target file exists but cannot be replaced because it is a non-empty directory
      IllegalStateException
      See Also:
    • copy

      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 copy
      f2 - the name of the target file.
      options - copy options, each of which is either LinkOption.NOFOLLOW_LINKS StandardCopyOption.COPY_ATTRIBUTES or StandardCopyOption.REPLACE_EXISTING
      Throws:
      UnsupportedOperationException - if an option is not recognized
      IOException - an IO error occurred, possibly due to permissions or a missing directory component
      FileAlreadyExistsException - if the target file exists but cannot be replaced becuase the REPLACE_EXISTING option was not specified
      DirectoryNotEmptyException - if the target file exists but cannot be replaced because it is a non-empty directory
      IllegalStateException
      See Also:
    • move

      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 move
      da - the directory accessor for the target file's location
      f2 - the file name for the new location.
      options - copy options, each of which is either StandardCopyOption.ATOMIC_MOVE or StandardCopyOption.REPLACE_EXISTING
      Throws:
      UnsupportedOperationException - if an option is not recognized
      IOException - an IO error occurred, possibly due to permissions or a missing directory component
      FileAlreadyExistsException - if the target file exists but cannot be replaced becuase the RPLACE_EXISTING option was not specified
      DirectoryNotEmptyException - if the target file exists but cannot be replaced because it is a non-empty directory
      IllegalStateException
      See Also:
    • copy

      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 copy
      da - the directory accessor
      f2 - the name of the target file.
      options - copy options, each of which is either LinkOption.NOFOLLOW_LINKS StandardCopyOption.COPY_ATTRIBUTES or StandardCopyOption.REPLACE_EXISTING
      Throws:
      UnsupportedOperationException - if an option is not recognized
      IOException - an IO error occurred, possibly due to permissions or a missing directory component
      FileAlreadyExistsException - if the target file exists but cannot be replaced becuase the REPLACE_EXISTING option was not specified
      DirectoryNotEmptyException - if the target file exists but cannot be replaced because it is a non-empty directory
      IllegalStateException
      See Also: