D.3 The KSLFileUtil Object

The KSLFileUtil Object supports the creation/deletion of files, directories, and working with file extensions. It also facilitates the reading and writing of 1-D and 2-D arrays. Figure D.2 illustrates the functions and properties of the KSLFileUtil object

KSLFileUtil Class

Figure D.2: KSLFileUtil Class

  • copyDirectory() and copyFile() facilitate path and file based copying
  • createFile(), createDirectories(), createPrintWriter(), all create according to their function names
  • deleteDirectory() will delete a directory based on a File or a Path
  • createCSVFileName(), createTxtFileName() will make a string that has the appropriate extension
  • isCSVFile(), isTeXFile(), isTextFile() check for the appropriate extension

As previously noted, there is a logger available for logging file interactions. A useful property is the programLaunchDirectory, which provides the path to the directory in which the program is executing.

The KSLFileUtil object also helps with array IO.

  • write(array, out: PrintWriter) has versions for working with arrays of primitives: Array<DoubleArray>, Array<IntArray>, DoubleArray, IntArray. The functions that write double values also have an optional argument to control formatting, df: DecimalFormat?. There are equivalent extension functions for Array<DoubleArray>, Array<IntArray>, DoubleArray, IntArray for writing to files.
  • scanToArray(path: Path) will read the values file associated with the path into an DoubleArray.
  • toCSVString(array: DoubleArray, df: DecimalFormat?) will format a line representing the array of data as a comma separated value string.

These functions are used in a number of other packages when working with data and files.