com.jdroid.java.utils
Class FileUtils

java.lang.Object
  extended by com.jdroid.java.utils.FileUtils

public abstract class FileUtils
extends Object

This class contains functions for working with files within the application.


Field Summary
static int BYTES_TO_MB
           
 
Constructor Summary
FileUtils()
           
 
Method Summary
static File checkFile(String filePath)
           
static ByteArrayInputStream copy(InputStream source)
           
static void copyStream(InputStream source, File destin)
           
static void copyStream(InputStream source, OutputStream destin)
           
static void copyStream(InputStream source, OutputStream destin, Boolean closeOutputStream)
           
static void createFile(String content, String parentPath, String fileName)
           
static File createTempDir()
           
static File createTempFile()
           
static boolean exist(String filePath)
           
static void forceDelete(File file)
          Deletes an instance of File even if it is a directory containing files.
If the file is a directory and has contents, then executes itself on every content.
static long getDirectorySize(File directory)
          Counts the size of a directory recursively (sum of the length of all files).
static float getDirectorySizeInMB(File directory)
           
static long getFileSize(File file)
           
static float getFileSizeInMB(File file)
           
static byte[] readAsBytes(File file)
          Reads the file and returns a byte array with all the information
static byte[] readAsBytes(InputStream inputStream)
          Reads the inputStream and returns a byte array with all the information
static boolean renameOrMove(File fileToBeMoved, File destination)
          Renames or moves a determined File instance to a destination defined by another File instance.
Differs from the File.renameTo(File) method in the fact that this method logs if the operation was successful.
static void safeClose(Closeable closeable)
           
static String toString(File file)
          Receives a File and iterates over all its lines and returns a String.
static String toString(InputStream in)
          Receives an InputStream and iterates over all its lines and returns a String.
static String toString(InputStream in, Boolean closeStream)
           
static File toTempFile(String content)
           
static File zipFile(String directoryToZipPath)
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

BYTES_TO_MB

public static final int BYTES_TO_MB
See Also:
Constant Field Values
Constructor Detail

FileUtils

public FileUtils()
Method Detail

readAsBytes

public static byte[] readAsBytes(InputStream inputStream)
                          throws IOException
Reads the inputStream and returns a byte array with all the information

Parameters:
inputStream - The inputStream to be read
Returns:
A byte array with all the inputStream's information
Throws:
IOException - The exception thrown when an error reading the inputStream happens

readAsBytes

public static byte[] readAsBytes(File file)
                          throws IOException
Reads the file and returns a byte array with all the information

Parameters:
file - The file to be read
Returns:
A byte array with all the file's information
Throws:
IOException - The exception thrown when an error reading the file happens

checkFile

public static File checkFile(String filePath)
Parameters:
filePath - The path to the file
Returns:
a file

exist

public static boolean exist(String filePath)
Parameters:
filePath - The file path to the file for verify the existence
Returns:
True if exist a file with in the file path

forceDelete

public static void forceDelete(File file)
Deletes an instance of File even if it is a directory containing files.
If the file is a directory and has contents, then executes itself on every content.

Parameters:
file - The File to be deleted.
See Also:
File.delete()

renameOrMove

public static boolean renameOrMove(File fileToBeMoved,
                                   File destination)
Renames or moves a determined File instance to a destination defined by another File instance.
Differs from the File.renameTo(File) method in the fact that this method logs if the operation was successful.

Parameters:
fileToBeMoved - The file to be renamed or moved.
destination - The File instance that denotes the new location
Returns:
boolean true if the file has been successfully renamed or moved.
See Also:
File.renameTo(File)

createTempFile

public static File createTempFile()

createTempDir

public static File createTempDir()

toTempFile

public static File toTempFile(String content)

createFile

public static void createFile(String content,
                              String parentPath,
                              String fileName)

toString

public static String toString(File file)
Receives a File and iterates over all its lines and returns a String.

Parameters:
file - The file
Returns:
The content of the file as String

toString

public static String toString(InputStream in,
                              Boolean closeStream)

toString

public static String toString(InputStream in)
Receives an InputStream and iterates over all its lines and returns a String.

Parameters:
in - the InputStream to be converted
Returns:
The content of the file as String

copyStream

public static void copyStream(InputStream source,
                              File destin)
Parameters:
source - the source InputStream
destin - the destin File

copyStream

public static void copyStream(InputStream source,
                              OutputStream destin,
                              Boolean closeOutputStream)
Parameters:
source - the source InputStream
destin - the destin OutputStream
closeOutputStream -

copyStream

public static void copyStream(InputStream source,
                              OutputStream destin)
Parameters:
source - the source InputStream
destin - the destin OutputStream

copy

public static ByteArrayInputStream copy(InputStream source)
Parameters:
source - the source InputStream
Returns:
the input stream that can be reset ByteArrayInputStream

zipFile

public static File zipFile(String directoryToZipPath)

safeClose

public static void safeClose(Closeable closeable)

getDirectorySize

public static long getDirectorySize(File directory)
Counts the size of a directory recursively (sum of the length of all files).

Parameters:
directory - directory to inspect, must not be null
Returns:
size of directory in bytes, 0 if directory is security restricted

getFileSize

public static long getFileSize(File file)

getDirectorySizeInMB

public static float getDirectorySizeInMB(File directory)

getFileSizeInMB

public static float getFileSizeInMB(File file)


Copyright © 2014. All rights reserved.