-
Notifications
You must be signed in to change notification settings - Fork 24
FileUtils
Stanislav Koshutsky edited this page Feb 15, 2017
·
4 revisions
A set of handy file(s) and folder(s) manipulation methods. Most used are:
copy - to copy a file to another folder
stringToFile - to write any text to a file
byteArrayToFile - byte[] usually is JPEG/PNG image representation
makeDirsForFile - ensure all folders of file's path has been created
isReadable - ensures that given file could be read (really has READ access)
isWritable - ensures that given file could be written (really has WRITE access)
deleteFiles - used to empty some folder, usually app's temp files or cache folder
Methods has been optimized for Samsung smartphones (contains a workaround for ENOENT error). Methods named so that you could understand what it does, like getAvailableSpace or mergeFiles etc.
public static void copy(final String src, final String dst) throws IOException
/**
* Copies an existing file to another destination
*
* @param src - String path of a file to copy from
* @param dst - String path of a file to copy to
* @throws IOException
*/
public static void copy(final File src, final File dst) throws IOException
/**
* Copies an existing file to another destination
*
* @param src - File to copy from
* @param dst - File to copy to
* @throws IOException
*/
public static synchronized void copySynchronized(final File src, final File dst) throws IOException
/**
* Copies a file to another destination
*
* @param src - File to copy from
* @param dst - File to copy to
* @throws IOException
*/
public static boolean stringToFile(final String data, final File targetFile)
/**
* Writes a String to a File (overwrites existing file)
*
* @param data - String to write to a file
* @param targetFile - target File
* @return true if all OK or false otherwise
*/
public static boolean stringToFile(final String data, final File targetFile, final boolean doAppend)
/**
* Writes a String to a File.
*
* @param data - String to write to a file
* @param targetFile - target File
* @param doAppend - append mode on/off
* @return true if all OK or false otherwise
*/
public static boolean streamToFile(final InputStream inputStream, final File targetFile)
/**
* Writes a stream to a file (overwrites existing file)
*
* @param inputStream - input stream
* @param targetFile - File to create
* @return true if all OK or false otherwise
*/
public static boolean streamToFile(final InputStream inputStream, final File targetFile, final boolean doAppend)
/**
* Writes a stream to a file
*
* @param inputStream - input stream
* @param targetFile - File to create
* @param doAppend - append mode on/off
* @return true if all OK or false otherwise
*/
public static boolean byteArrayOutputStreamToFile(final ByteArrayOutputStream bos, final File targetFile)
/**
* Writes a byte array to a file (overwrites existing file)
*
* @param bos - ByteArrayOutputStream
* @param targetFile - File to create
* @return true if all OK or false otherwise
*/
public static boolean byteArrayOutputStreamToFile(final ByteArrayOutputStream bos, final File targetFile, final boolean doAppend) {
/**
* Writes a byte array to a file.
*
* @param bos - ByteArrayOutputStream
* @param targetFile - File to create
* @param doAppend - append mode on/off
* @return true if all OK or false otherwise
*/
public static boolean byteArrayToFile(final byte[] array, final File targetFile) {
/**
* Writes a byte array to a file (overwrites existing file)
*
* @param array - byte[]
* @param targetFile - File to create
* @return true if all OK or false otherwise
*/
public static boolean byteArrayToFile(final byte[] array, final File targetFile, final boolean doAppend) {
/**
* Writes a byte array to a file.
*
* @param array - byte[]
* @param targetFile - File to create
* @param doAppend - append mode on/off
* @return true if all OK or false otherwise
*/
public static boolean intentDataToFile(final Context context, final Uri uri, final String targetFile) {
/**
* Method creates a File from intent's data
*
* @param context - Context
* @param uri - Uri taken from Intent.detData()
* @param targetFile - File to create
* @return true if all OK or false otherwise
*/
public static boolean intentDataToFile(final Context context, final Uri uri, final File targetFile) {
/**
* Method creates a File from intent's data
*
* @param context - Context
* @param uri - Uri taken from Intent.detData()
* @param targetFile - File to create
* @return true if all OK or false otherwise
*/
public static boolean makeDirsForFile(final String file) {
/**
* Method creates the FILE's path dirs and returns true if succeed. The difference
* from File.mkdirs() is that mkdirs() returns false in both cases:
* it can't create path OR path already exists. This method will return
* false if path could not be created only.
*
* @param file - File only the not a Directory!
* @return true if file's path created/exists or false if path could not be created
*/
public static boolean makeDirsForFile(final File file) {
/**
* Method creates the FILE's path dirs and returns true if succeed. The difference
* from File.mkdirs() is that mkdirs() returns false in both cases:
* it can't create path OR path already exists. This method will return
* false if path could not be created only.
*
* @param file - File only the not a Directory!
* @return true if file's path created/exists or false if path could not be created
*/
public static boolean isWritable(final String file) {
/**
* Method checks if file could be written/deleted or created. Does not create path/mkdirs so if path doesn't exists returns false.
*
* @param file - File only not a Directory!
* @return true if file's is writable or false otherwise
*/
public static boolean isWritable(final File file) {
/**
* Method checks if file could be written/deleted or created. Does not create path/mkdirs so if path doesn't exists returns false.
*
* @param file - File only not a Directory!
* @return true if file's is writable or false otherwise
*/
public static boolean isWritable(final String file, final boolean makeDirs) {
/**
* Method checks if file could be written/deleted or created. Method will create path/mkdirs if makeDirs set to true and file's path doesn't exists.
*
* @param file - File only not a Directory!
* @return true if file's is writable or false otherwise
*/
public static boolean isWritable(final File file, final boolean makeDirs) {
/**
* Method checks if file could be written/deleted or created. Method will create path/mkdirs if makeDirs set to true and file's path doesn't exists.
*
* @param file - File only not a Directory!
* @return true if file's is writable or false otherwise
*/
public static boolean isReadable(final String file) {
/**
* Method checks if file could be read or created
*
* @param file - File only not a Directory!
* @return true if file's is writable or false otherwise
*/
public static boolean isReadable(final File file) {
/**
* Method checks if file could be read or created
*
* @param file - File only not a Directory!
* @return true if file's is writable or false otherwise
*/
public static boolean sync(final OutputStream stream) {
/**
* Method ensures about file creation from stream. For Samsung like devices
*
* @param stream - OutputStream
* @return true if all OK or false otherwise
*/
public static boolean sync(final FileOutputStream stream) {
/**
* Method ensures about file creation from stream. For Samsung like devices
*
* @param stream - FileOutputStream
* @return true if all OK or false otherwise
*/
public static boolean syncAndClose(final FileOutputStream stream) {
/**
* Method ensures about file creation from stream. For Samsung like devices
*
* @param stream - FileOutputStream
* @return true if all OK or false otherwise
*/
public static boolean deleteFilesAndDirsRecursive(final String file) {
/**
* Method deletes all files and subdirectories recursively from given directory.
* Returns true if all files deleted false if at least one doesn't
*
* @param file - File which represents a directory where to delete all files and dirs
*/
public static boolean deleteFilesAndDirsRecursive(final File directory) {
/**
* Method deletes all files and subdirectories recursively from given directory.
* Returns true if all files deleted false if at least one doesn't
*
* @param directory - File which represents a directory where to delete all files and dirs
*/
public static boolean deleteFiles(final File targetDir) {
/**
* Method deletes all files only (but NOT subdirectories) from given directory.
* Returns true if all files deleted false if at least one doesn't
*
* @param targetDir - File with represents a Directory where to delete all files
*/
public static boolean isFileExists(final String path) {
/**
* Check if given path is a File and if it exists
*
* @param path - the path to check
* @return boolean
*/
public static long getAvailableSpace(final File file) {
/**
* Returns the free space in bytes available at the given file's path
* or -1 if storage is not available (UNMOUNTED, etc)
*
* @return long
*/
public static long getAvailableSpace(final Uri uri) {
/**
* Returns the free space in bytes available at the given file's path
* or -1 if storage is not available (UNMOUNTED, etc)
*
* @return long
*/
public static long getAvailableSpace(String mFileRootPath) {
/**
* Returns the free space in bytes available at the given file's path
* or -1 if storage is not available (UNMOUNTED, etc)
*
* @return long
*/
public static String[] getFilenamesByExtension(final File directory, final String extension) {
/**
* returns list of given directory files as String[]
*
* @param directory - File representing directory
* @param extension - String representing file's extension, e.g. "png" or ".jpeg" (including or excluding starting dot)
* @return String [] with files list
*/
public static File[] getFilesByExtension(final File directory, final String extension) {
/**
* creates File[] containing files located in given directory
*
* @param directory - File representing directory
* @param extension - String representing file's extension, e.g. "png" or ".jpeg" (including or excluding starting dot)
* @return File[] with files list
*/
public static File getFileFromUri(final Context context, final Uri uri) {
/**
* Retrieves a File path represented by given Uri
*
* @param context
* @param uri
* @return
*/
public static String getPathFromUri(final Context context, final Uri uri) {
/**
* Retrieves a String path represented by given Uri. Uses DocumentsContract for API 19.
*
* @param context
* @param uri
* @return
*/
public static String getDataColumn(final Context context, final Uri uri, final String selection, final String[] selectionArgs) {
/**
* Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders.
*
* @param context The context.
* @param uri The Uri to query.
* @param selection (Optional) Filter used in the query.
* @param selectionArgs (Optional) Selection arguments used in the query.
* @return The value of the _data column, which is typically a file path.
*/
public static boolean isExternalStorageDocument(final Uri uri) {
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is ExternalStorageProvider.
*/
public static boolean isDownloadsDocument(final Uri uri) {
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is DownloadsProvider.
*/
public static boolean isMediaDocument(final Uri uri) {
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is MediaProvider.
*/
public static void mergeFiles(final File file1, final File file2, final File outputFile) {
/**
* Merge two files into a new file
*
* @param file1 - File to merge (will be first in resulting file)
* @param file2 - File to merge (will be second in resulting file)
* @param outputFile - resulting File
*/
public static void appendFileToFile(final File file1, final File file2) {
/**
* Appends one file to another.
*
* @param file1 - file to append to
* @param file2 - file being append to file1
*/
public static String getBase64EncodedFile(final File fileToEncode) {
/**
* Returns a Base64.DEFAULT encoded String representation of given file
* Aware of OOM: large files will probably cause it.
*
* @param fileToEncode
* @return
*/
public static class ExtensionFilter implements FilenameFilter {
/**
* Represents extension for filtering for File.list()
*/