Class FileSystemResource

java.lang.Object
com.helger.io.resource.FileSystemResource
All Implemented Interfaces:
com.helger.base.io.iface.IHasInputStream, com.helger.base.io.iface.IHasInputStreamAndReader, com.helger.base.io.iface.IHasOutputStream, com.helger.base.io.iface.IHasOutputStreamAndWriter, IReadableResource, IReadWriteResource, IResourceBase, IWritableResource

@NotThreadSafe public class FileSystemResource extends Object implements IReadWriteResource
Implementation of the IReadableResource and IWritableResource interfaces for file system objects.
Author:
Philip Helger
  • Constructor Details

    • FileSystemResource

      public FileSystemResource(@NonNull URI aURI)
      Create a new file system resource from a URI.
      Parameters:
      aURI - The URI to create the resource from. May not be null.
    • FileSystemResource

      public FileSystemResource(@NonNull String sParentPath, @NonNull String sChildPath)
      Create a new file system resource from a parent path and a child path.
      Parameters:
      sParentPath - The parent path. May not be null.
      sChildPath - The child path. May not be null.
    • FileSystemResource

      public FileSystemResource(@NonNull String sFilename)
      Create a new file system resource from a filename.
      Parameters:
      sFilename - The filename of the resource. May not be null.
    • FileSystemResource

      public FileSystemResource(@NonNull File aParentFile, String sChildPath)
      Create a new file system resource from a parent file and a child path.
      Parameters:
      aParentFile - The parent file. May not be null.
      sChildPath - The child path.
    • FileSystemResource

      public FileSystemResource(@NonNull File aFile)
      Create a new file system resource from a File. The file path is made absolute and cleaned.
      Parameters:
      aFile - The file to use. May not be null.
    • FileSystemResource

      public FileSystemResource(@NonNull Path aPath)
      Create a new file system resource from a Path.
      Parameters:
      aPath - The path to use. May not be null.
  • Method Details

    • getResourceID

      public @NonNull String getResourceID()
      Get the unique resource ID of this file system resource.
      Specified by:
      getResourceID in interface IResourceBase
      Returns:
      The absolute path of the file. Never null.
    • getPath

      public @NonNull String getPath()
      Get the absolute path of this file system resource.
      Specified by:
      getPath in interface IResourceBase
      Returns:
      The absolute path. Never null.
    • getInputStream

      public @Nullable FileInputStream getInputStream()
      Get an input stream for reading from this resource.
      Specified by:
      getInputStream in interface com.helger.base.io.iface.IHasInputStream
      Returns:
      null if the file does not exist or cannot be opened.
    • getReader

      public @Nullable Reader getReader(@NonNull Charset aCharset)
      Specified by:
      getReader in interface com.helger.base.io.iface.IHasInputStreamAndReader
    • isReadMultiple

      public final boolean isReadMultiple()
      Check if this resource can be read multiple times. File system resources always return true.
      Specified by:
      isReadMultiple in interface com.helger.base.io.iface.IHasInputStream
      Returns:
      Always true.
    • getOutputStream

      public @Nullable FileOutputStream getOutputStream(@NonNull com.helger.base.io.EAppend eAppend)
      Get an output stream for writing to this resource.
      Specified by:
      getOutputStream in interface com.helger.base.io.iface.IHasOutputStream
      Parameters:
      eAppend - Appending mode. May not be null.
      Returns:
      null if the file could not be opened.
    • getWriter

      public @Nullable Writer getWriter(@NonNull Charset aCharset, @NonNull com.helger.base.io.EAppend eAppend)
      Specified by:
      getWriter in interface com.helger.base.io.iface.IHasOutputStreamAndWriter
    • isWriteMultiple

      public final boolean isWriteMultiple()
      Check if this resource can be written multiple times. File system resources always return true.
      Specified by:
      isWriteMultiple in interface com.helger.base.io.iface.IHasOutputStream
      Returns:
      Always true.
    • exists

      public boolean exists()
      Check if the underlying file exists.
      Specified by:
      exists in interface IResourceBase
      Returns:
      true if the file exists, false otherwise.
    • getAsURL

      public @Nullable URL getAsURL()
      Get this resource as a URL.
      Specified by:
      getAsURL in interface IResourceBase
      Returns:
      null if the conversion to URL failed.
    • getAsFile

      public @NonNull File getAsFile()
      Get this resource as a File.
      Specified by:
      getAsFile in interface IResourceBase
      Returns:
      The underlying file. Never null.
    • getReadableCloneForPath

      public @NonNull FileSystemResource getReadableCloneForPath(@NonNull String sPath)
      Create a new FileSystemResource for a different path.
      Specified by:
      getReadableCloneForPath in interface IReadableResource
      Parameters:
      sPath - The new path to use. May not be null.
      Returns:
      A new FileSystemResource instance. Never null.
    • getWritableCloneForPath

      public @NonNull FileSystemResource getWritableCloneForPath(@NonNull String sPath)
      Create a new writable FileSystemResource for a different path.
      Specified by:
      getWritableCloneForPath in interface IWritableResource
      Parameters:
      sPath - The new path to use. May not be null.
      Returns:
      A new FileSystemResource instance. Never null.
    • canRead

      public boolean canRead()
      Tests whether the application can read the file denoted by this abstract pathname.
      Returns:
      true if and only if the file specified by this abstract pathname exists and can be read by the application; false otherwise
    • canWrite

      public boolean canWrite()
      Tests whether the application can modify the file denoted by this abstract pathname.
      Returns:
      true if and only if the file system actually contains a file denoted by this abstract pathname and the application is allowed to write to the file; false otherwise.
    • canExecute

      public boolean canExecute()
      Tests whether the application can execute the file denoted by this abstract pathname.
      Returns:
      true if and only if the abstract pathname exists and the application is allowed to execute the file
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • toString

      public String toString()
      Overrides:
      toString in class Object