Class IOHelper

java.lang.Object
eu.svjatoslav.commons.file.IOHelper

public class IOHelper extends Object
  • Constructor Details

    • IOHelper

      public IOHelper()
  • Method Details

    • deleteRecursively

      public static void deleteRecursively(File file) throws IOException
      Deletes files and directories recursively. Does not follow symlinks.
      Parameters:
      file - directory to delete with entire contents.
      Throws:
      IOException - if filesystem error happens
    • getFileContents

      public static byte[] getFileContents(File file) throws IOException
      Throws:
      IOException
    • getFileContentsAsString

      public static String getFileContentsAsString(File file) throws IOException
      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 overwrite
      newContent - new content
      Returns:
      true if file was overwritten.
      Throws:
      FileNotFoundException - if file is not found.
      IOException - if error happens during file IO.
    • saveToFile

      public static void saveToFile(File file, byte[] content) throws IOException
      Throws:
      IOException
    • saveToFile

      public static void saveToFile(File file, String content) throws IOException
      Throws:
      IOException