KSLFileUtil

Provides some basic file utilities. Additional utilities can be found in Google Guava and Apache Commons IO. However, this basic IO provides basic needs without external libraries and is integrated withe KSL functionality.

Properties

Link copied to clipboard
val logger: KLogger

Use for general logging

Link copied to clipboard

Returns the path to the directory that the program was launched from on the OS. This call may throw a SecurityException if the system information is not accessible. Uses System property "user.dir"

Link copied to clipboard

Returns the directory that the program was launched from on the OS as a string. This call may throw a SecurityException if the system information is not accessible. Uses System property "user.dir"

Link copied to clipboard

System.out as a PrintWriter

Functions

Link copied to clipboard

Opens a file dialog to select a file to open. The dialog will open in the directory that the program was launched from.

Link copied to clipboard
fun copyDirectory(source: File, destination: File)
fun copyDirectory(source: Path, destination: Path)
Link copied to clipboard

Makes a String that has the form name.csv

Link copied to clipboard

Will throw an IOException if something goes wrong in the creation.

Link copied to clipboard
fun createFile(pathToFile: Path): File

fun createFile(fileName: String): File

Creates a file in the directory that the program was launched from. Parent directories are created if they do not exist.

fun createFile(parentDir: Path, fileName: String): File

Creates a file within a specific parent directory. Parent directories are created if they do not exist.

Link copied to clipboard
fun createFileName(theName: String?, theFileExtension: String?): String

Makes a String that has the form 'name.ext' If an extension already exists it is replaced.

Link copied to clipboard
fun createFileWithExtension(fileName: String?, extension: String?, parentDir: Path = KSL.outDir): File

Creates a file with a guaranteed extension in the specified parent directory. If the file name is null, a temporary name is generated.

Link copied to clipboard

Makes a LogPrintWriter from the given File. IOExceptions are caught and logged. If the file exists it will be written over.

Makes a PrintWriter from the given path, any IOExceptions are caught and logged.

Link copied to clipboard
fun createOutFile(fileName: String): File

Creates a file in the standard KSL output directory. Parent directories are created if they do not exist.

Link copied to clipboard

Makes a PrintWriter from the given File. IOExceptions are caught and logged. If the file exists it is written over.

Makes a PrintWriter from the given path, any IOExceptions are caught and logged. The path must be to a file, not a directory. If the directories that are on the path do not exist, they are created. If the referenced file exists it is written over.

Makes the file in the directory that the program launched within

Link copied to clipboard
fun createSubDirectory(mainDir: Path, dirName: String): Path

Creates a subdirectory within the supplied main directory.

Link copied to clipboard
fun createTemporaryHTMLFile(fileName: String, dir: Path = KSL.outDir): File

Creates a temporary HTML file in the specified directory with the given file name. The file will have a .html extension. If the directory does not exist, it will be created.

Link copied to clipboard

Makes a String that has the form name.txt

Link copied to clipboard
fun deleteDirectory(directoryToBeDeleted: File): Boolean

Recursively deletes

fun deleteDirectory(pathToDir: Path): Boolean

Deletes the directory represented by the path and all of its contents. If the path is not a directory, it is deleted as a file.

Link copied to clipboard

This method will check for the dot '.' occurrence in the given filename. If it exists, then it will find the last position of the dot '.' and return the characters after that, the characters after the last dot '.' known as the file extension. Special Cases:

Link copied to clipboard

This method will check for the dot '.' occurrence in the given filename. If it exists, then it will find the last position of the dot '.' and return the characters after that, the characters after the last dot '.' known as the file extension. Special Cases:

Link copied to clipboard
fun hasCSVExtension(pathToFile: Path): Boolean
Link copied to clipboard
fun isCSVFile(pathToFile: Path?): Boolean
Link copied to clipboard
fun isCSVFileName(fileName: String?): Boolean
Link copied to clipboard
fun isTeXFile(pathToFile: Path?): Boolean
Link copied to clipboard
fun isTexFileName(fileName: String?): Boolean
Link copied to clipboard
fun isTextFile(pathToFile: Path?): Boolean
Link copied to clipboard
fun isTextFileName(fileName: String?): Boolean
Link copied to clipboard
fun openInBrowser(file: File)

Opens the file in the browser. The file must be an HTML file and the system must have a default browser configured. If the file is not an HTML file or if there is no default browser, an IOException may be thrown.

fun openInBrowser(fileName: String, html: String, dir: Path = KSL.outDir): File

Opens the HTML file in the browser. The file is created in the specified directory with the given file name. The file will have a .html extension. If the directory does not exist, it will be created.

Link copied to clipboard
fun removeFileExtension(filename: String?, removeAllExtensions: Boolean): String?
Link copied to clipboard
Link copied to clipboard
fun scanToArray(pathToFile: Path): DoubleArray

Assumes that the file holds doubles with each value on a different line 1.0 4.0 2.0 etc

Link copied to clipboard
fun toCSVString(array: DoubleArray, df: DecimalFormat? = null): String
Link copied to clipboard
fun write(array: Array<IntArray>, out: PrintWriter = SOUT)
fun write(array: IntArray, out: PrintWriter = SOUT)
fun write(array: Array<DoubleArray>, out: PrintWriter = SOUT, df: DecimalFormat? = null)
fun write(array: DoubleArray, out: PrintWriter = SOUT, df: DecimalFormat? = null)

Allows writing directly to a known PrintWriter. Facilitates writing to the file before or after the array is written

fun write(list: List<DoubleArray>, out: PrintWriter = SOUT, df: DecimalFormat? = null)

Allows writing directly to a known PrintWriter. Facilitates writing to the file before or after the list is written

Link copied to clipboard
fun writeToFile(array: Array<IntArray>, pathToFile: Path)
fun writeToFile(array: Array<IntArray>, fileName: String)
fun writeToFile(array: Array<DoubleArray>, pathToFile: Path, df: DecimalFormat? = null)
fun writeToFile(array: Array<DoubleArray>, fileName: String, df: DecimalFormat? = null)

Writes the data in the array to rows in the file, each element in a row is separated by a comma

fun writeToFile(array: IntArray, pathToFile: Path)
fun writeToFile(array: IntArray, fileName: String)
fun writeToFile(array: DoubleArray, pathToFile: Path, df: DecimalFormat? = null): Path
fun writeToFile(array: DoubleArray, fileName: String, df: DecimalFormat? = null): Path

Writes the data in the array to rows in the file, each row with one data point