Package 

Interface FileSystem


  • 
    public interface FileSystem
    
                        

    Access to read and write files on a hierarchical data store. Most callers should use the implementation, which uses the host machine's local file system. Alternate implementations may be used to inject faults (for testing) or to transform stored data (to add encryption, for example).

    All operations on a file system are racy. For example, guarding a call to source with exists does not guarantee that FileNotFoundException will not be thrown. The file may be moved between the two calls!

    This interface is less ambitious than java.nio.file.FileSystem introduced in Java 7. It lacks important features like file watching, metadata, permissions, and disk space information. In exchange for these limitations, this interface is easier to implement and works on all versions of Java and Android.

    • Method Summary

      Modifier and Type Method Description
      abstract Source source(File file) Reads from {@code file}.
      abstract Sink sink(File file) Writes to {@code file}, discarding any data already present.
      abstract Sink appendingSink(File file) Writes to {@code file}, appending if data is already present.
      abstract void delete(File file) Deletes {@code file} if it exists.
      abstract boolean exists(File file) Returns true if {@code file} exists on the file system.
      abstract long size(File file) Returns the number of bytes stored in {@code file}, or 0 if it does not exist.
      abstract void rename(File from, File to) Renames {@code from} to {@code to}.
      abstract void deleteContents(File directory) Recursively delete the contents of {@code directory}.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • sink

         abstract Sink sink(File file)

        Writes to {@code file}, discarding any data already present. Creates parent directories ifnecessary.

      • appendingSink

         abstract Sink appendingSink(File file)

        Writes to {@code file}, appending if data is already present. Creates parent directories ifnecessary.

      • delete

         abstract void delete(File file)

        Deletes {@code file} if it exists. Throws if the file exists and cannot be deleted.

      • exists

         abstract boolean exists(File file)

        Returns true if {@code file} exists on the file system.

      • size

         abstract long size(File file)

        Returns the number of bytes stored in {@code file}, or 0 if it does not exist.

      • rename

         abstract void rename(File from, File to)

        Renames {@code from} to {@code to}. Throws if the file cannot be renamed.

      • deleteContents

         abstract void deleteContents(File directory)

        Recursively delete the contents of {@code directory}. Throws an IOException if any file couldnot be deleted, or if {@code dir} is not a readable directory.