public class ParseFileUtils
extends java.lang.Object
| Modifier and Type | Field and Description |
|---|---|
static long |
ONE_KB
The number of bytes in a kilobyte.
|
static long |
ONE_MB
The number of bytes in a megabyte.
|
| Constructor and Description |
|---|
ParseFileUtils() |
| Modifier and Type | Method and 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 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 org.json.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.nio.charset.Charset encoding) |
static java.lang.String |
readFileToString(java.io.File file,
java.lang.String 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,
org.json.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.nio.charset.Charset encoding) |
static void |
writeStringToFile(java.io.File file,
java.lang.String string,
java.lang.String encoding) |
public static final long ONE_KB
public static final long ONE_MB
public static byte[] readFileToByteArray(java.io.File file)
throws java.io.IOException
file - the file to read, must not be nullnulljava.io.IOException - in case of an I/O errorpublic static java.io.FileInputStream openInputStream(java.io.File file)
throws java.io.IOException
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.
file - the file to open for input, must not be nullFileInputStream for the specified filejava.io.FileNotFoundException - if the file does not existjava.io.IOException - if the file object is a directoryjava.io.IOException - if the file cannot be readpublic static void writeByteArrayToFile(java.io.File file,
byte[] data)
throws java.io.IOException
NOTE: As from v1.3, the parent directories of the file will be created if they do not exist.
file - the file to write todata - the content to write to the filejava.io.IOException - in case of an I/O errorpublic static java.io.FileOutputStream openOutputStream(java.io.File file)
throws java.io.IOException
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.
file - the file to open for output, must not be nullFileOutputStream for the specified filejava.io.IOException - if the file object is a directoryjava.io.IOException - if the file cannot be written tojava.io.IOException - if a parent directory needs creating but that failspublic static void moveFile(java.io.File srcFile,
java.io.File destFile)
throws java.io.IOException
When the destination file is on another file system, do a "copy and delete".
srcFile - the file to be moveddestFile - the destination filejava.lang.NullPointerException - if source or destination is nulljava.io.IOException - if source or destination is invalidjava.io.IOException - if an IO error occurs moving the filepublic static void copyFile(java.io.File srcFile,
java.io.File destFile)
throws java.io.IOException
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.
srcFile - an existing file to copy, must not be nulldestFile - the new file, must not be nulljava.lang.NullPointerException - if source or destination is nulljava.io.IOException - if source or destination is invalidjava.io.IOException - if an IO error occurs during copyingjava.io.IOException - if the output file length is not the same as the input file length after the copy completescopyFile(File, File, boolean)public static void copyFile(java.io.File srcFile,
java.io.File destFile,
boolean preserveFileDate)
throws java.io.IOException
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.
srcFile - an existing file to copy, must not be nulldestFile - the new file, must not be nullpreserveFileDate - true if the file date of the copy
should be the same as the originaljava.lang.NullPointerException - if source or destination is nulljava.io.IOException - if source or destination is invalidjava.io.IOException - if an IO error occurs during copyingjava.io.IOException - if the output file length is not the same as the input file length after the copy completesdoCopyFile(File, File, boolean)public static void deleteDirectory(java.io.File directory)
throws java.io.IOException
directory - directory to deletejava.io.IOException - in case deletion is unsuccessfulpublic static boolean deleteQuietly(java.io.File file)
The difference between File.delete() and this method are:
file - file or directory to delete, can be nulltrue if the file or directory was deleted, otherwise
falsepublic static void cleanDirectory(java.io.File directory)
throws java.io.IOException
directory - directory to cleanjava.io.IOException - in case cleaning is unsuccessfulpublic static void forceDelete(java.io.File file)
throws java.io.IOException
The difference between File.delete() and this method are:
file - file or directory to delete, must not be nulljava.lang.NullPointerException - if the directory is nulljava.io.FileNotFoundException - if the file was not foundjava.io.IOException - in case deletion is unsuccessfulpublic static boolean isSymlink(java.io.File file)
throws java.io.IOException
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)
file - the file to checkjava.io.IOException - if an IO error occurs while checking the filepublic static java.lang.String readFileToString(java.io.File file,
java.nio.charset.Charset encoding)
throws java.io.IOException
java.io.IOExceptionpublic static java.lang.String readFileToString(java.io.File file,
java.lang.String encoding)
throws java.io.IOException
java.io.IOExceptionpublic static void writeStringToFile(java.io.File file,
java.lang.String string,
java.nio.charset.Charset encoding)
throws java.io.IOException
java.io.IOExceptionpublic static void writeStringToFile(java.io.File file,
java.lang.String string,
java.lang.String encoding)
throws java.io.IOException
java.io.IOExceptionpublic static org.json.JSONObject readFileToJSONObject(java.io.File file)
throws java.io.IOException,
org.json.JSONException
JSONObject. The file is always closed.java.io.IOExceptionorg.json.JSONExceptionpublic static void writeJSONObjectToFile(java.io.File file,
org.json.JSONObject json)
throws java.io.IOException
JSONObject to a file creating the file if it does not exist.java.io.IOException