Package eu.svjatoslav.commons.file
Class IOHelper
java.lang.Object
eu.svjatoslav.commons.file.IOHelper
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic voiddeleteRecursively(File file) Deletes files and directories recursively.static byte[]getFileContents(File file) static StringgetFileContentsAsString(File file) Expects file content to be in UTF-8 encoding.static booleanoverwriteFileIfContentDiffers(File file, byte[] newContent) Compares new file content with old file content.static voidsaveToFile(File file, byte[] content) static voidsaveToFile(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:
trueif file was overwritten.- Throws:
FileNotFoundException- if file is not found.IOException- if error happens during file IO.
-
saveToFile
- Throws:
IOException
-
saveToFile
- Throws:
IOException
-