Package eu.svjatoslav.commons.file
Class IOHelper
java.lang.Object
eu.svjatoslav.commons.file.IOHelper
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionstatic void
deleteRecursively
(File file) Deletes files and directories recursively.static byte[]
getFileContents
(File file) static String
getFileContentsAsString
(File file) Expects file content to be in UTF-8 encoding.static boolean
overwriteFileIfContentDiffers
(File file, byte[] newContent) Compares new file content with old file content.static void
saveToFile
(File file, byte[] content) static void
saveToFile
(File file, String content)
-
Constructor Details
-
IOHelper
public IOHelper()
-
-
Method Details
-
deleteRecursively
Deletes files and directories recursively. Does not follow symlinks.- Parameters:
file
- directory to delete with entire contents.- Throws:
IOException
- if filesystem error happens
-
getFileContents
- Throws:
IOException
-
getFileContentsAsString
Expects file content to be in UTF-8 encoding.- Parameters:
file
- file to read- Returns:
- File content
- Throws:
IOException
- when file reading fails.
-
overwriteFileIfContentDiffers
public static boolean overwriteFileIfContentDiffers(File file, byte[] newContent) throws IOException Compares new file content with old file content. If content in equal, then leaves file as-is. If content differs, then overrides file with the new content.- Parameters:
file
- file to potentially overwritenewContent
- new content- Returns:
true
if file was overwritten.- Throws:
FileNotFoundException
- if file is not found.IOException
- if error happens during file IO.
-
saveToFile
- Throws:
IOException
-
saveToFile
- Throws:
IOException
-