Package com.parse

Class ParseFileUtils


  • public class ParseFileUtils
    extends java.lang.Object
    General file manipulation utilities.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static long ONE_KB
      The number of bytes in a kilobyte.
      static long ONE_MB
      The number of bytes in a megabyte.
    • Constructor Summary

      Constructors 
      Constructor Description
      ParseFileUtils()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static void cleanDirectory​(java.io.File directory)
      Cleans a directory without deleting it.
      static void copyFile​(java.io.File srcFile, java.io.File destFile)
      Copies a file to a new location preserving the file date.
      static void copyFile​(java.io.File srcFile, java.io.File destFile, boolean preserveFileDate)
      Copies a file to a new location.
      static void deleteDirectory​(java.io.File directory)
      Deletes a directory recursively.
      static boolean deleteQuietly​(java.io.File file)
      Deletes a file, never throwing an exception.
      static void forceDelete​(java.io.File file)
      Deletes a file.
      static void getAllNestedFiles​(java.lang.String directoryName, java.util.List<java.io.File> files)
      Get all files path from an given directory (including sub-directory).
      static boolean isSymlink​(java.io.File file)
      Determines whether the specified file is a Symbolic Link rather than an actual file.
      static void moveFile​(java.io.File srcFile, java.io.File destFile)
      Moves a file.
      static java.io.FileInputStream openInputStream​(java.io.File file)
      Opens a FileInputStream for the specified file, providing better error messages than simply calling new FileInputStream(file).
      static java.io.FileOutputStream openOutputStream​(java.io.File file)
      Opens a FileOutputStream for the specified file, checking and creating the parent directory if it does not exist.
      static byte[] readFileToByteArray​(java.io.File file)
      Reads the contents of a file into a byte array.
      static JSONObject readFileToJSONObject​(java.io.File file)
      Reads the contents of a file into a JSONObject.
      static java.lang.String readFileToString​(java.io.File file, java.lang.String encoding)  
      static java.lang.String readFileToString​(java.io.File file, java.nio.charset.Charset encoding)  
      static void writeByteArrayToFile​(java.io.File file, byte[] data)
      Writes a byte array to a file creating the file if it does not exist.
      static void writeJSONObjectToFile​(java.io.File file, JSONObject json)
      Writes a JSONObject to a file creating the file if it does not exist.
      static void writeStringToFile​(java.io.File file, java.lang.String string, java.lang.String encoding)  
      static void writeStringToFile​(java.io.File file, java.lang.String string, java.nio.charset.Charset encoding)  
      static void writeUriToFile​(java.io.File file, Uri uri)
      Writes a content uri to a file creating the file if it does not exist.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • ONE_KB

        public static final long ONE_KB
        The number of bytes in a kilobyte.
        See Also:
        Constant Field Values
      • ONE_MB

        public static final long ONE_MB
        The number of bytes in a megabyte.
        See Also:
        Constant Field Values
    • Constructor Detail

      • ParseFileUtils

        public ParseFileUtils()
    • Method Detail

      • readFileToByteArray

        public static byte[] readFileToByteArray​(java.io.File file)
                                          throws java.io.IOException
        Reads the contents of a file into a byte array. The file is always closed.
        Parameters:
        file - the file to read, must not be null
        Returns:
        the file contents, never null
        Throws:
        java.io.IOException - in case of an I/O error
        Since:
        Commons IO 1.1
      • openInputStream

        public static java.io.FileInputStream openInputStream​(java.io.File file)
                                                       throws java.io.IOException
        Opens a FileInputStream for the specified file, providing better error messages than simply calling new FileInputStream(file).

        At the end of the method either the stream will be successfully opened, or an exception will have been thrown.

        An exception is thrown if the file does not exist. An exception is thrown if the file object exists but is a directory. An exception is thrown if the file exists but cannot be read.

        Parameters:
        file - the file to open for input, must not be null
        Returns:
        a new FileInputStream for the specified file
        Throws:
        java.io.FileNotFoundException - if the file does not exist
        java.io.IOException - if the file object is a directory
        java.io.IOException - if the file cannot be read
        Since:
        Commons IO 1.3
      • writeByteArrayToFile

        public static void writeByteArrayToFile​(java.io.File file,
                                                byte[] data)
                                         throws java.io.IOException
        Writes a byte array to a file creating the file if it does not exist.

        NOTE: As from v1.3, the parent directories of the file will be created if they do not exist.

        Parameters:
        file - the file to write to
        data - the content to write to the file
        Throws:
        java.io.IOException - in case of an I/O error
        Since:
        Commons IO 1.1
      • writeUriToFile

        public static void writeUriToFile​(java.io.File file,
                                          Uri uri)
                                   throws java.io.IOException
        Writes a content uri to a file creating the file if it does not exist.

        NOTE: As from v1.3, the parent directories of the file will be created if they do not exist.

        Parameters:
        file - the file to write to
        uri - the content uri with data to write to the file
        Throws:
        java.io.IOException - in case of an I/O error
        Since:
        Commons IO 1.1
      • openOutputStream

        public static java.io.FileOutputStream openOutputStream​(java.io.File file)
                                                         throws java.io.IOException
        Opens a FileOutputStream for the specified file, checking and creating the parent directory if it does not exist.

        At the end of the method either the stream will be successfully opened, or an exception will have been thrown.

        The parent directory will be created if it does not exist. The file will be created if it does not exist. An exception is thrown if the file object exists but is a directory. An exception is thrown if the file exists but cannot be written to. An exception is thrown if the parent directory cannot be created.

        Parameters:
        file - the file to open for output, must not be null
        Returns:
        a new FileOutputStream for the specified file
        Throws:
        java.io.IOException - if the file object is a directory
        java.io.IOException - if the file cannot be written to
        java.io.IOException - if a parent directory needs creating but that fails
        Since:
        Commons IO 1.3
      • moveFile

        public static void moveFile​(java.io.File srcFile,
                                    java.io.File destFile)
                             throws java.io.IOException
        Moves a file.

        When the destination file is on another file system, do a "copy and delete".

        Parameters:
        srcFile - the file to be moved
        destFile - the destination file
        Throws:
        java.lang.NullPointerException - if source or destination is null
        java.io.IOException - if source or destination is invalid
        java.io.IOException - if an IO error occurs moving the file
        Since:
        1.4
      • copyFile

        public static void copyFile​(java.io.File srcFile,
                                    java.io.File destFile)
                             throws java.io.IOException
        Copies a file to a new location preserving the file date.

        This method copies the contents of the specified source file to the specified destination file. The directory holding the destination file is created if it does not exist. If the destination file exists, then this method will overwrite it.

        Note: This method tries to preserve the file's last modified date/times using File.setLastModified(long), however it is not guaranteed that the operation will succeed. If the modification operation fails, no indication is provided.

        Parameters:
        srcFile - an existing file to copy, must not be null
        destFile - the new file, must not be null
        Throws:
        java.lang.NullPointerException - if source or destination is null
        java.io.IOException - if source or destination is invalid
        java.io.IOException - if an IO error occurs during copying
        java.io.IOException - if the output file length is not the same as the input file length after the copy completes
        See Also:
        copyFile(File, File, boolean)
      • copyFile

        public static void copyFile​(java.io.File srcFile,
                                    java.io.File destFile,
                                    boolean preserveFileDate)
                             throws java.io.IOException
        Copies a file to a new location.

        This method copies the contents of the specified source file to the specified destination file. The directory holding the destination file is created if it does not exist. If the destination file exists, then this method will overwrite it.

        Note: Setting preserveFileDate to true tries to preserve the file's last modified date/times using File.setLastModified(long), however it is not guaranteed that the operation will succeed. If the modification operation fails, no indication is provided.

        Parameters:
        srcFile - an existing file to copy, must not be null
        destFile - the new file, must not be null
        preserveFileDate - true if the file date of the copy should be the same as the original
        Throws:
        java.lang.NullPointerException - if source or destination is null
        java.io.IOException - if source or destination is invalid
        java.io.IOException - if an IO error occurs during copying
        java.io.IOException - if the output file length is not the same as the input file length after the copy completes
        See Also:
        doCopyFile(File, File, boolean)
      • getAllNestedFiles

        public static void getAllNestedFiles​(java.lang.String directoryName,
                                             java.util.List<java.io.File> files)
        Get all files path from an given directory (including sub-directory).
        Parameters:
        directoryName - given directory name.
        files - where all the files will be stored.
      • deleteDirectory

        public static void deleteDirectory​(java.io.File directory)
                                    throws java.io.IOException
        Deletes a directory recursively.
        Parameters:
        directory - directory to delete
        Throws:
        java.io.IOException - in case deletion is unsuccessful
      • deleteQuietly

        public static boolean deleteQuietly​(java.io.File file)
        Deletes a file, never throwing an exception. If file is a directory, delete it and all sub-directories.

        The difference between File.delete() and this method are:

        • A directory to be deleted does not have to be empty.
        • No exceptions are thrown when a file or directory cannot be deleted.
        Parameters:
        file - file or directory to delete, can be null
        Returns:
        true if the file or directory was deleted, otherwise false
        Since:
        1.4
      • cleanDirectory

        public static void cleanDirectory​(java.io.File directory)
                                   throws java.io.IOException
        Cleans a directory without deleting it.
        Parameters:
        directory - directory to clean
        Throws:
        java.io.IOException - in case cleaning is unsuccessful
      • forceDelete

        public static void forceDelete​(java.io.File file)
                                throws java.io.IOException
        Deletes a file. If file is a directory, delete it and all sub-directories.

        The difference between File.delete() and this method are:

        • A directory to be deleted does not have to be empty.
        • You get exceptions when a file or directory cannot be deleted. (java.io.File methods returns a boolean)
        Parameters:
        file - file or directory to delete, must not be null
        Throws:
        java.lang.NullPointerException - if the directory is null
        java.io.FileNotFoundException - if the file was not found
        java.io.IOException - in case deletion is unsuccessful
      • isSymlink

        public static boolean isSymlink​(java.io.File file)
                                 throws java.io.IOException
        Determines whether the specified file is a Symbolic Link rather than an actual file.

        Will not return true if there is a Symbolic Link anywhere in the path, only if the specific file is.

        For code that runs on Java 1.7 or later, use the following method instead:
        boolean java.nio.file.Files.isSymbolicLink(Path path)

        Parameters:
        file - the file to check
        Returns:
        true if the file is a Symbolic Link
        Throws:
        java.io.IOException - if an IO error occurs while checking the file
        Since:
        2.0
      • readFileToString

        public static java.lang.String readFileToString​(java.io.File file,
                                                        java.nio.charset.Charset encoding)
                                                 throws java.io.IOException
        Throws:
        java.io.IOException
      • readFileToString

        public static java.lang.String readFileToString​(java.io.File file,
                                                        java.lang.String encoding)
                                                 throws java.io.IOException
        Throws:
        java.io.IOException
      • writeStringToFile

        public static void writeStringToFile​(java.io.File file,
                                             java.lang.String string,
                                             java.nio.charset.Charset encoding)
                                      throws java.io.IOException
        Throws:
        java.io.IOException
      • writeStringToFile

        public static void writeStringToFile​(java.io.File file,
                                             java.lang.String string,
                                             java.lang.String encoding)
                                      throws java.io.IOException
        Throws:
        java.io.IOException
      • readFileToJSONObject

        public static JSONObject readFileToJSONObject​(java.io.File file)
                                               throws java.io.IOException,
                                                      JSONException
        Reads the contents of a file into a JSONObject. The file is always closed.
        Throws:
        java.io.IOException
        JSONException
      • writeJSONObjectToFile

        public static void writeJSONObjectToFile​(java.io.File file,
                                                 JSONObject json)
                                          throws java.io.IOException
        Writes a JSONObject to a file creating the file if it does not exist.
        Throws:
        java.io.IOException