Package nl.rrd.wool.utils
Class FileUtils
- java.lang.Object
-
- nl.rrd.wool.utils.FileUtils
-
public class FileUtils extends Object
This class contains utility methods related to the file system.
-
-
Constructor Summary
Constructors Constructor Description FileUtils()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidclearDirectory(File dir)Deletes the content of the specified directory.static voidcopyFile(File source, File dest)Copies the specified source file to the specified destination file.static voidcopyFile(File source, File dest, long start, long len)Copies bytes from the specified source file to the specified destination file.static FilecreateTempFile(File dir, String prefix, String ext)Creates a temporary file in the specified directory.static voiddeleteTree(File dir)Deletes the specified directory including all its contents.static voiddownloadFile(URL source, File dest)static voidmkdir(File dir)Creates the specified directory and all non-existing parent directories.static byte[]readFileBytes(File file)Reads the content of the specified file as a byte array.static byte[]readFileBytes(InputStream input)Reads the content of the specified file as a byte array.static byte[]readFileBytes(URL url)Reads the content of the specified file as a byte array.static StringreadFileString(File file)Reads the content of the specified file as a string.static StringreadFileString(InputStream input)Reads the content of the specified file as a string.static StringreadFileString(Reader reader)Reads the content of the specified file as a string.static StringreadFileString(URL url)Reads the content of the specified file as a string.static voidtruncate(File file, long len)Truncates the specified file so it won't be longer than the specified length.static voidwriteFileString(File file, String content)Writes a string to a file with encoding UTF-8.static voidwriteFileString(OutputStream output, String content)Writes a string to an output stream with encoding UTF-8.
-
-
-
Method Detail
-
mkdir
public static void mkdir(File dir) throws IOException
Creates the specified directory and all non-existing parent directories.- Parameters:
dir- the directory- Throws:
IOException- if the directory can't be created
-
truncate
public static void truncate(File file, long len) throws IOException
Truncates the specified file so it won't be longer than the specified length. This method will copy the file to a temporary file and then copy back at most "len" bytes.- Parameters:
file- the filelen- the length- Throws:
IOException- if a reading/writing error occurs
-
createTempFile
public static File createTempFile(File dir, String prefix, String ext) throws IOException
Creates a temporary file in the specified directory. The file name will be formatted as "prefix.number.ext", where number is a sequence of eight hexadecimal digits. This method will use a file name that doesn't already exist and it atomically creates an empty file with that name.- Parameters:
dir- the directoryprefix- the prefixext- the extension- Returns:
- the file
- Throws:
IOException- if a writing error occurs
-
copyFile
public static void copyFile(File source, File dest) throws IOException
Copies the specified source file to the specified destination file.- Parameters:
source- the source filedest- the destination file- Throws:
IOException- if a read/write error occurs
-
copyFile
public static void copyFile(File source, File dest, long start, long len) throws IOException
Copies bytes from the specified source file to the specified destination file.- Parameters:
source- the source filedest- the destination filestart- the index of the first byte to copylen- the number of bytes to copy- Throws:
IOException- if a read/write error occurs
-
downloadFile
public static void downloadFile(URL source, File dest) throws IOException
- Throws:
IOException
-
deleteTree
public static void deleteTree(File dir) throws IOException
Deletes the specified directory including all its contents. Ifdiris a file, it will just delete that file.- Parameters:
dir- the directory or file- Throws:
IOException- if a file or directory can't be deleted
-
clearDirectory
public static void clearDirectory(File dir) throws IOException
Deletes the content of the specified directory. If the directory doesn't exist, this method has no effect.- Parameters:
dir- the directory- Throws:
IOException- if the specified path is not a directory or if a file or directory can't be deleted
-
readFileBytes
public static byte[] readFileBytes(File file) throws IOException
Reads the content of the specified file as a byte array.- Parameters:
file- the file- Returns:
- the content
- Throws:
IOException- if a reading error occurs
-
readFileBytes
public static byte[] readFileBytes(URL url) throws IOException
Reads the content of the specified file as a byte array.- Parameters:
url- the URL for the file- Returns:
- the content
- Throws:
IOException- if a reading error occurs
-
readFileBytes
public static byte[] readFileBytes(InputStream input) throws IOException
Reads the content of the specified file as a byte array.- Parameters:
input- the input stream from the file- Returns:
- the content
- Throws:
IOException- if a reading error occurs
-
readFileString
public static String readFileString(File file) throws IOException
Reads the content of the specified file as a string. It assumes character encoding UTF-8.- Parameters:
file- the file- Returns:
- the content
- Throws:
IOException- if a reading error occurs
-
readFileString
public static String readFileString(URL url) throws IOException
Reads the content of the specified file as a string. It assumes character encoding UTF-8.- Parameters:
url- the URL for the file- Returns:
- the content
- Throws:
IOException- if a reading error occurs
-
readFileString
public static String readFileString(InputStream input) throws IOException
Reads the content of the specified file as a string. It assumes character encoding UTF-8.- Parameters:
input- the input stream from the file- Returns:
- the content
- Throws:
IOException- if a reading error occurs
-
readFileString
public static String readFileString(Reader reader) throws IOException
Reads the content of the specified file as a string.- Parameters:
reader- the reader from the file- Returns:
- the content
- Throws:
IOException- if a reading error occurs
-
writeFileString
public static void writeFileString(File file, String content) throws IOException
Writes a string to a file with encoding UTF-8.- Parameters:
file- the filecontent- the content string to write- Throws:
IOException- if a writing error occurs
-
writeFileString
public static void writeFileString(OutputStream output, String content) throws IOException
Writes a string to an output stream with encoding UTF-8.- Parameters:
output- the output streamcontent- the content string to write- Throws:
IOException- if a writing error occurs
-
-