Package utility

Class UtilIO


  • public class UtilIO
    extends Object
    Utility class will handle common sequences of I/O, issue messages, and throw errors as necessary.
    Version:
    1.0
    • Field Detail

      • FILE_DELIM

        public static String FILE_DELIM
        File delimiter
      • UTF8

        public static String UTF8
        UTF8 identifier
      • OS_WIN32

        public static String OS_WIN32
        Windows OS identifier
      • OS_MAC

        public static String OS_MAC
        Mac OS identifier
      • OS_LINUX

        public static String OS_LINUX
        Linux OS identifier
    • Constructor Detail

      • UtilIO

        public UtilIO()
    • Method Detail

      • isDir

        public static Boolean isDir​(String dirOrFile)
        Return whether input file is a directory or file
        Parameters:
        dirOrFile - File path
        Returns:
        true if file path is a directory, false otherwise
      • normalizeExtension

        public static String normalizeExtension​(String extension)
        Take an extension and prefix with a '.' identifier
        Parameters:
        extension - Extension to normalize
        Returns:
        A string '.bin' for input 'bin' for example
      • existsSync

        public static Boolean existsSync​(String file)
        Wraps Files. Exists so that we don't have to import fs unnecessarily
        Parameters:
        file - File to validate existence against
        Returns:
        true Ff file exists
      • createDirSync

        public static void createDirSync​(String dir)
                                  throws IOException
        Create a directory if it does not yet exist synchronously.
        Parameters:
        dir - Directory to create
        Throws:
        IOException - I/O error processing
      • createDirsSync

        public static void createDirsSync​(String dir)
                                   throws IOException
        Create all needed directories for an input directory in the form of: first/second/third where first will contain director second and second will contain directory third
        Parameters:
        dir - Directory to create all subdirectories for
        Throws:
        IOException - I/O error processing
      • createDirsSyncFromFilePath

        public static void createDirsSyncFromFilePath​(String filePath)
                                               throws IOException
        Create all necessary directories for a fully qualified file and its path, for example, if filePath = oneDir/twoDir/threeDir/file.txt, oneDir, twoDir, and threeDir will be created.
        Parameters:
        filePath - File path
        Throws:
        IOException - I/O error processing
      • createSymlinkToDir

        public static void createSymlinkToDir​(String newSymLinkPath,
                                              String existingDirPath)
                                       throws Exception
        Create a symbolic link to a directory. If the symbolic link already exists, re-create it with the specified target directory.
        Parameters:
        newSymLinkPath - The path new symbolic link to be created
        existingDirPath - The path the existing directory that we will link to
        Throws:
        Exception - error processing
      • mkdirp

        public static void mkdirp​(String dir)
                           throws IOException
        Uses the fs-extra package to create a directory (and all subdirectories)
        Parameters:
        dir - The directory (do not include a file name)
        Throws:
        IOException - I/O error processing
      • readFileSyncBinary

        public static BufferedReader readFileSyncBinary​(String file)
                                                 throws IOException
        Read file as a stream or specify encoding.
        Parameters:
        file - File to read
        Returns:
        Buffer the content of the file
        Throws:
        IOException - I/O error processing
      • readFileSyncAsString

        public static String readFileSyncAsString​(String file,
                                                  Boolean normalizeNewLines)
        Read file as a string with line normalization or specify encoding.
        Parameters:
        file - File to read
        normalizeNewLines - True to toggle Remove Windows line endings (\r\n) in favor of \n
        Returns:
        A string with the content of the file
      • createReadStream

        public static String createReadStream​(String file)
                                       throws IOException
        Create a Readable stream from a file
        Parameters:
        file - The file from which to create a read stream
        Returns:
        A string with the content of the file
        Throws:
        IOException - I/O error processing
      • createWriteStream

        public static FileOutputStream createWriteStream​(String file)
                                                  throws FileNotFoundException
        Create a Node.js Readable stream from a file
        Parameters:
        file - The file from which to create a read stream
        Returns:
        A string with the content of the file
        Throws:
        FileNotFoundException - file not found error
      • processNewlines

        public static String processNewlines​(String original)
        Process a string so that its line endings are operating system appropriate before you save it to disk (basically, if the user is on Windows, change \n to \r\n)
        Parameters:
        original - Original input
        Returns:
        A string with input with removed newlines
      • getDefaultTextEditor

        public static String getDefaultTextEditor()
        Get default text editor for a given operating system
        Returns:
        A string with text editor