Interface FileSystem


  • public interface FileSystem
    Access to read and write files on a hierarchical data store. Most callers should use the SYSTEM 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(java.io.File) with exists(java.io.File) does not guarantee that FileNotFoundException will not be thrown. The file may be moved between the two calls!

    This interface is less ambitious than 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.

    Copied from OkHttp 3.14.2: https://github.com/square/okhttp/blob/ b8b6ee831c65208940c741f8e091ff02425566d5/okhttp/src/main/java/okhttp3/internal/io/FileSystem.java

    • Field Summary

      Fields 
      Modifier and Type Field Description
      static FileSystem SYSTEM
      The host machine's local file system.
    • Method Summary

      All Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      okio.Sink appendingSink​(java.io.File file)
      Writes to file, appending if data is already present.
      void delete​(java.io.File file)
      Deletes file if it exists.
      void deleteContents​(java.io.File directory)
      Recursively delete the contents of directory.
      boolean exists​(java.io.File file)
      Returns true if file exists on the file system.
      void rename​(java.io.File from, java.io.File to)
      Renames from to to.
      okio.Sink sink​(java.io.File file)
      Writes to file, discarding any data already present.
      long size​(java.io.File file)
      Returns the number of bytes stored in file, or 0 if it does not exist.
      okio.Source source​(java.io.File file)
      Reads from file.
    • Field Detail

      • SYSTEM

        static final FileSystem SYSTEM
        The host machine's local file system.
    • Method Detail

      • source

        okio.Source source​(java.io.File file)
                    throws java.io.FileNotFoundException
        Reads from file.
        Throws:
        java.io.FileNotFoundException
      • sink

        okio.Sink sink​(java.io.File file)
                throws java.io.FileNotFoundException
        Writes to file, discarding any data already present. Creates parent directories if necessary.
        Throws:
        java.io.FileNotFoundException
      • appendingSink

        okio.Sink appendingSink​(java.io.File file)
                         throws java.io.FileNotFoundException
        Writes to file, appending if data is already present. Creates parent directories if necessary.
        Throws:
        java.io.FileNotFoundException
      • delete

        void delete​(java.io.File file)
             throws java.io.IOException
        Deletes file if it exists. Throws if the file exists and cannot be deleted.
        Throws:
        java.io.IOException
      • exists

        boolean exists​(java.io.File file)
        Returns true if file exists on the file system.
      • size

        long size​(java.io.File file)
        Returns the number of bytes stored in file, or 0 if it does not exist.
      • rename

        void rename​(java.io.File from,
                    java.io.File to)
             throws java.io.IOException
        Renames from to to. Throws if the file cannot be renamed.
        Throws:
        java.io.IOException
      • deleteContents

        void deleteContents​(java.io.File directory)
                     throws java.io.IOException
        Recursively delete the contents of directory. Throws an IOException if any file could not be deleted, or if dir is not a readable directory.
        Throws:
        java.io.IOException