Package utility
Class UtilIO
- java.lang.Object
-
- utility.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
-
-
Constructor Summary
Constructors Constructor Description UtilIO()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
createDirsSync(String dir)
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 thirdstatic void
createDirsSyncFromFilePath(String filePath)
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.static void
createDirSync(String dir)
Create a directory if it does not yet exist synchronously.static String
createReadStream(String file)
Create a Readable stream from a filestatic void
createSymlinkToDir(String newSymLinkPath, String existingDirPath)
Create a symbolic link to a directory.static FileOutputStream
createWriteStream(String file)
Create a Node.js Readable stream from a filestatic Boolean
existsSync(String file)
Wraps Files.static String
getDefaultTextEditor()
Get default text editor for a given operating systemstatic Boolean
isDir(String dirOrFile)
Return whether input file is a directory or filestatic void
mkdirp(String dir)
Uses the fs-extra package to create a directory (and all subdirectories)static String
normalizeExtension(String extension)
Take an extension and prefix with a '.' identifierstatic 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)static String
readFileSyncAsString(String file, Boolean normalizeNewLines)
Read file as a string with line normalization or specify encoding.static BufferedReader
readFileSyncBinary(String file)
Read file as a stream or specify encoding.
-
-
-
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 createdexistingDirPath
- 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 readnormalizeNewLines
- 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
-
-