Class FileUtil


  • public class FileUtil
    extends Object
    Utility do to certain common file operations. These are mostly aimed at smaller files. It uses NIO when possible.

    Created by Jeff Gaynor
    on 1/26/21 at 7:10 AM

    • Constructor Detail

      • FileUtil

        public FileUtil()
    • Method Detail

      • readFileAsBinary

        public static String readFileAsBinary​(String fileName)
                                       throws Throwable
        Read a (possibly binary) file and convert the contents to a base64 escaped string.
        Parameters:
        fileName -
        Returns:
        Throws:
        Throwable
      • writeFileAsBinary

        public static void writeFileAsBinary​(String filename,
                                             String contents)
                                      throws Throwable
        Compliment to readFileAsBinary(String). This will take a base64 encoded string, decode it to a byte array and write the result to a file.
        Parameters:
        filename -
        contents -
        Throws:
        Throwable
      • readFileAsString

        public static String readFileAsString​(String fileName)
                                       throws Throwable
        Read a (text) file in as a long string.
        Parameters:
        fileName -
        Returns:
        Throws:
        Throwable
      • readFileAsString

        public static String readFileAsString​(InputStream inputStream)
                                       throws Throwable
        Reads an InputStream as a single string. This is useful when reading a resource as an input stream.
        Parameters:
        inputStream -
        Returns:
        Throws:
        Throwable
      • readFileAsLines

        public static List<String> readFileAsLines​(InputStream inputStream)
                                            throws Throwable
        Reads an InputStream as a set of lines. This is useful when reading a resource as an input stream.
        Parameters:
        inputStream -
        Returns:
        Throws:
        Throwable
      • readFileAsLines

        public static List<String> readFileAsLines​(String fileName)
                                            throws Throwable
        Read a (text) file in as a list of strings, one per line.
        Parameters:
        fileName -
        Returns:
        Throws:
        Throwable