Class FileUtil


  • public class FileUtil
    extends java.lang.Object
    Provides utility methods to work files and folders.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
      static class  FileUtil.StrictMode
      Provides strict mode equivalents to methods in FileUtil.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void closeQuietly​(java.io.Closeable closeable)
      Closes the given Closeable object ignoring any resulting exceptions.
      static boolean delete​(java.io.File path)
      Deletes a file or folder, even if the folder is not empty.
      static boolean exists​(java.lang.String path)
      Checks if specified path points to a file or directory that exists on the local filesystem.
      static java.io.File getExternalDirectory​(android.content.Context context, java.lang.String folderType, java.lang.String name)
      Gets the path to a directory on the primary shared/external storage device where the application can place persistent files it owns.
      static java.lang.String getFileName​(java.lang.String url)
      Gets the file name from a URL string.
      static boolean hasFileScheme​(java.net.URI uri)
      Checks if the URI has a file scheme.
      static boolean isDirectory​(java.lang.String path)
      Checks if specified path points to a directory that exists on the local filesystem.
      static boolean isFile​(java.lang.String path)
      Checks if specified path points to a file that exists on the local filesystem.
      static boolean isFileValid​(java.io.File path)
      Checks if the specified path is valid, meaning it exists or can be created, has read permissions and has write permissions
      static java.io.File makeReadWriteDirectory​(java.io.File path)
      Checks if the specified path is a directory that can be read from and written into.
      static void saveInputStream​(java.io.File target, java.io.InputStream source)
      Utility method to save the input stream to the file provided.
      • Methods inherited from class java.lang.Object

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

      • makeReadWriteDirectory

        @NonNull
        public static java.io.File makeReadWriteDirectory​(@Nullable
                                                          java.io.File path)
        Checks if the specified path is a directory that can be read from and written into. If the directory does not exist, then this method will try to create a readable/writable directory.
        Parameters:
        path - the path to check/create.
        Returns:
        reference to the validated path.
        Throws:
        java.lang.IllegalArgumentException - if the download path is invalid or if the download path does not have read or write permissions.
      • isFileValid

        public static boolean isFileValid​(@Nullable
                                          java.io.File path)
        Checks if the specified path is valid, meaning it exists or can be created, has read permissions and has write permissions
        Parameters:
        path - File to check if is valid.
        Returns:
        true if the path is valid.
      • getExternalDirectory

        @NonNull
        public static java.io.File getExternalDirectory​(@NonNull
                                                        android.content.Context context,
                                                        @Nullable
                                                        java.lang.String folderType,
                                                        @Nullable
                                                        java.lang.String name)

        Gets the path to a directory on the primary shared/external storage device where the application can place persistent files it owns. These files are internal to the applications, and not typically visible to the user as media.

        Please note that this method will neither check if selected directory exists nor whether it is readable or writable.

        Parameters:
        context - the context
        folderType - the type of files directory to return. May be null for the root of the files directory or one of the following constants for a subdirectory: Environment.DIRECTORY_MUSIC, Environment.DIRECTORY_PODCASTS, Environment.DIRECTORY_RINGTONES, Environment.DIRECTORY_ALARMS, Environment.DIRECTORY_NOTIFICATIONS, Environment.DIRECTORY_PICTURES, or Environment.DIRECTORY_MOVIES.
        name - an optional subfolder name within the selected folder type.
        Returns:
        the path to the selected directory.
        See Also:
        makeReadWriteDirectory(File)
      • delete

        public static boolean delete​(@NonNull
                                     java.io.File path)
        Deletes a file or folder, even if the folder is not empty.
        Parameters:
        path - the path to be deleted.
        Returns:
        false if the file or folder was found, but could not be deleted.
      • getFileName

        public static java.lang.String getFileName​(java.lang.String url)
        Gets the file name from a URL string.
        Parameters:
        url - the url string
        Returns:
        the file name
      • closeQuietly

        public static void closeQuietly​(@Nullable
                                        java.io.Closeable closeable)
        Closes the given Closeable object ignoring any resulting exceptions.
        Parameters:
        closeable - the object to be closed.
      • saveInputStream

        public static void saveInputStream​(@NonNull
                                           java.io.File target,
                                           @NonNull
                                           java.io.InputStream source)
                                    throws java.io.IOException
        Utility method to save the input stream to the file provided.
        Parameters:
        source - the input stream
        target - the file to save the input stream
        Throws:
        java.io.IOException - if the stream cannot be saved.
      • isFile

        public static boolean isFile​(@Nullable
                                     java.lang.String path)
        Checks if specified path points to a file that exists on the local filesystem.
        Parameters:
        path - the string path to be checked.
        Returns:
        true is the path points to a local file.
      • isDirectory

        public static boolean isDirectory​(@Nullable
                                          java.lang.String path)
        Checks if specified path points to a directory that exists on the local filesystem.
        Parameters:
        path - the string path to be checked.
        Returns:
        true is the path points to a local file.
      • exists

        public static boolean exists​(@Nullable
                                     java.lang.String path)
        Checks if specified path points to a file or directory that exists on the local filesystem.
        Parameters:
        path - the string path to be checked.
        Returns:
        true is the path points to a local file or directory.
      • hasFileScheme

        public static boolean hasFileScheme​(java.net.URI uri)
        Checks if the URI has a file scheme.
        Parameters:
        uri - the URI object to check.
        Returns:
        true if the uri contains a file scheme, false otherwise.