Package jsl.utilities
Class JSLFileUtil
- java.lang.Object
-
- jsl.utilities.JSLFileUtil
-
public class JSLFileUtil extends java.lang.Object
Provides some basic file utilities. Addtional 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 JSL functionality.
-
-
Field Summary
Fields Modifier and Type Field Description static org.slf4j.Logger
LOGGER
for logging
-
Constructor Summary
Constructors Constructor Description JSLFileUtil()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static void
copyDirectory(java.io.File source, java.io.File destination)
static void
copyDirectory(java.nio.file.Path source, java.nio.file.Path destination)
static java.nio.file.Path
createDirectories(java.nio.file.Path path)
Will throw an IOException if something goes wrong in the creation.static boolean
deleteDirectory(java.io.File directoryToBeDeleted)
Recursively deletesstatic boolean
deleteDirectory(java.nio.file.Path pathToDir)
static java.util.Optional<java.lang.String>
getExtensionByStringFileName(java.lang.String filename)
This method will check for the dot ‘.' occurrence in the given filename.static java.util.Optional<java.lang.String>
getExtensionFromPath(java.nio.file.Path pathToFile)
This method will check for the dot ‘.' occurrence in the given filename.static java.nio.file.Path
getProgramLaunchDirectory()
Returns the path to the directory that the program was launched from on the OS.static java.lang.String
getProgramLaunchDirectoryAsString()
Returns the directory that the program was launched from on the OS as a string.static boolean
hasCSVExtension(java.nio.file.Path pathToFile)
static boolean
isCSVFile(java.nio.file.Path pathToFile)
static boolean
isCSVFileName(java.lang.String fileName)
static boolean
isTeXFile(java.nio.file.Path pathToFile)
static boolean
isTexFileName(java.lang.String fileName)
static boolean
isTextFile(java.nio.file.Path pathToFile)
static boolean
isTextFileName(java.lang.String fileName)
static java.lang.String
makeCSVFileName(java.lang.String name)
Makes a String that has the form name.csvstatic java.io.File
makeFile(java.nio.file.Path pathToFile)
static java.lang.String
makeFileName(java.lang.String name, java.lang.String ext)
Makes a String that has the form name.ext If an extension already exists it is replaced.static LogPrintWriter
makeLogPrintWriter(java.io.File file)
Makes a LogPrintWriter from the given File.static LogPrintWriter
makeLogPrintWriter(java.nio.file.Path pathToFile)
Makes a PrintWriter from the given path, any IOExceptions are caught and logged.static int
makePDFFromLaTeX(java.lang.String pdfCmdString, java.io.File file)
Creates a PDF representation of a LaTeX file within the with the given name.static int
makePDFFromLaTeX(java.lang.String pdfcmd, java.lang.String dirname, java.lang.String filename)
Creates a PDF representation of a LaTeX file within the provided directory with the given name.static java.io.PrintWriter
makePrintWriter(java.io.File file)
Makes a PrintWriter from the given File.static java.io.PrintWriter
makePrintWriter(java.lang.String fileName)
Makes the file in the directory that the program launched withinstatic java.io.PrintWriter
makePrintWriter(java.nio.file.Path pathToFile)
Makes a PrintWriter from the given path, any IOExceptions are caught and logged.static java.nio.file.Path
makeSubDirectory(java.nio.file.Path mainDir, java.lang.String dirName)
Creates a sub-directory within the supplied main directory.static java.lang.String
makeTxtFileName(java.lang.String name)
Makes a String that has the form name.txtstatic void
openFile(java.io.File file)
Uses Desktop.getDesktop() to open the filestatic java.lang.String
removeFileExtension(java.lang.String filename, boolean removeAllExtensions)
static java.lang.String
removeLastFileExtension(java.lang.String fileName)
-
-
-
Method Detail
-
getProgramLaunchDirectoryAsString
public static java.lang.String getProgramLaunchDirectoryAsString()
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"- Returns:
- the path as a string
-
getProgramLaunchDirectory
public static java.nio.file.Path getProgramLaunchDirectory()
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"- Returns:
- the path to the directory
-
hasCSVExtension
public static boolean hasCSVExtension(java.nio.file.Path pathToFile)
- Parameters:
pathToFile
- the path to the file- Returns:
- true if the path exists and the extension is csv
-
makePrintWriter
public static java.io.PrintWriter makePrintWriter(java.nio.file.Path pathToFile)
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.- Parameters:
pathToFile
- the path to the file that will be underneath the PrintWriter, must not be null- Returns:
- the returned PrintWriter, or a PrintWriter wrapping System.out if some problem occurs
-
makePrintWriter
public static java.io.PrintWriter makePrintWriter(java.io.File file)
Makes a PrintWriter from the given File. IOExceptions are caught and logged. If the file exists it is written over.- Parameters:
file
- the file support the returned PrintWriter, must not be null- Returns:
- the PrintWriter, may be System.out if an IOException occurred
-
makePrintWriter
public static java.io.PrintWriter makePrintWriter(java.lang.String fileName)
Makes the file in the directory that the program launched within- Parameters:
fileName
- the name of the file to make- Returns:
- the created PrintWriter, may be System.out if an IOException occurred
-
makeLogPrintWriter
public static LogPrintWriter makeLogPrintWriter(java.nio.file.Path pathToFile)
Makes a PrintWriter from the given path, any IOExceptions are caught and logged.- Parameters:
pathToFile
- the path to the file that will be underneath the PrintWriter, must not be null- Returns:
- the returned PrintWriter, or System.out if some IOException occurred
-
makeLogPrintWriter
public static LogPrintWriter makeLogPrintWriter(java.io.File file)
Makes a LogPrintWriter from the given File. IOExceptions are caught and logged. If the file exists it will be written over.- Parameters:
file
- the file support the returned PrintWriter, must not be null- Returns:
- the LogPrintWriter, may be a PrintWriter wrapping System.out if an IOException occurred
-
createDirectories
public static java.nio.file.Path createDirectories(java.nio.file.Path path)
Will throw an IOException if something goes wrong in the creation.- Parameters:
path
- the path to the directory to create- Returns:
- the same path after creating the intermediate directories
-
makeSubDirectory
public static java.nio.file.Path makeSubDirectory(java.nio.file.Path mainDir, java.lang.String dirName)
Creates a sub-directory within the supplied main directory.- Parameters:
mainDir
- a path to the directory to hold the sub-directory, must not be nulldirName
- the name of the sub-directory, must not be null- Returns:
- the path to the sub-directory, or mainDir, if something went wrong
-
makeFile
public static java.io.File makeFile(java.nio.file.Path pathToFile)
- Parameters:
pathToFile
- the path to the file, must not be null and must not be a directory- Returns:
- the reference to the File
-
openFile
public static void openFile(java.io.File file) throws java.io.IOException
Uses Desktop.getDesktop() to open the file- Parameters:
file
- the file- Throws:
java.io.IOException
- if file cannot be opened
-
makePDFFromLaTeX
public static int makePDFFromLaTeX(java.lang.String pdfcmd, java.lang.String dirname, java.lang.String filename) throws java.io.IOException, java.lang.InterruptedException
Creates a PDF representation of a LaTeX file within the provided directory with the given name.- Parameters:
pdfcmd
- the command for making the pdf within the OSdirname
- must not be nullfilename
- must not be null, must have .tex extension- Returns:
- the process exit value
- Throws:
java.io.IOException
- if file does not exist or end with .texjava.lang.InterruptedException
- if it was interrupted
-
makePDFFromLaTeX
public static int makePDFFromLaTeX(java.lang.String pdfCmdString, java.io.File file) throws java.io.IOException, java.lang.InterruptedException
Creates a PDF representation of a LaTeX file within the with the given name. Uses pdflatex if it exists- Parameters:
pdfCmdString
- must not be null, the appropriate OS system command to convert tex filefile
- must not be null, must have .tex extension- Returns:
- the process exit value
- Throws:
java.io.IOException
- if file does not exist or end with .texjava.lang.InterruptedException
- if it was interrupted
-
removeLastFileExtension
public static java.lang.String removeLastFileExtension(java.lang.String fileName)
- Parameters:
fileName
- the string path representation of the file- Returns:
- the string without the extensions
-
removeFileExtension
public static java.lang.String removeFileExtension(java.lang.String filename, boolean removeAllExtensions)
- Parameters:
filename
- the string path representation of the fileremoveAllExtensions
- if true all extensions including the last are removed- Returns:
- the string without the extensions
-
getExtensionFromPath
public static java.util.Optional<java.lang.String> getExtensionFromPath(java.nio.file.Path pathToFile)
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:No extension – this method will return an empty String Only extension – this method will return the String after the dot, e.g. gitignore See www.baeldung.com/java-file-extension Used here to avoid having to use external library
- Parameters:
pathToFile
- the name of the file that has the extension- Returns:
- an optional holding the string of the extension or null
-
getExtensionByStringFileName
public static java.util.Optional<java.lang.String> getExtensionByStringFileName(java.lang.String filename)
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:No extension – this method will return an empty String Only extension – this method will return the String after the dot, e.g. “gitignore” See www.baeldung.com/java-file-extension Used here to avoid having to use external library
- Parameters:
filename
- the name of the file that has the extension- Returns:
- an optional holding the string of the extension or null
-
isTextFileName
public static boolean isTextFileName(java.lang.String fileName)
- Parameters:
fileName
- the name of the file as a string- Returns:
- true if the extension for the file is txt or TXT
-
isTextFile
public static boolean isTextFile(java.nio.file.Path pathToFile)
- Parameters:
pathToFile
-- Returns:
- true if extension on path is txt
-
isCSVFileName
public static boolean isCSVFileName(java.lang.String fileName)
- Parameters:
fileName
- the name of the file as a string- Returns:
- true if the extension for the file is txt or TXT
-
isCSVFile
public static boolean isCSVFile(java.nio.file.Path pathToFile)
- Parameters:
pathToFile
-- Returns:
- true if extension on path is csv
-
isTexFileName
public static boolean isTexFileName(java.lang.String fileName)
- Parameters:
fileName
- the name of the file as a string- Returns:
- true if the extension for the file is tex
-
isTeXFile
public static boolean isTeXFile(java.nio.file.Path pathToFile)
- Parameters:
pathToFile
-- Returns:
- true if extension on path is tex
-
makeCSVFileName
public static java.lang.String makeCSVFileName(java.lang.String name)
Makes a String that has the form name.csv- Parameters:
name
- the name- Returns:
- the formed String
-
makeTxtFileName
public static java.lang.String makeTxtFileName(java.lang.String name)
Makes a String that has the form name.txt- Parameters:
name
- the name- Returns:
- the formed String
-
makeFileName
public static java.lang.String makeFileName(java.lang.String name, java.lang.String ext)
Makes a String that has the form name.ext If an extension already exists it is replaced.- Parameters:
name
- the nameext
- the extension- Returns:
- the String
-
deleteDirectory
public static boolean deleteDirectory(java.nio.file.Path pathToDir)
-
deleteDirectory
public static boolean deleteDirectory(java.io.File directoryToBeDeleted)
Recursively deletes- Parameters:
directoryToBeDeleted
- the file reference to the directory to delete- Returns:
- true if deleted
-
copyDirectory
public static void copyDirectory(java.nio.file.Path source, java.nio.file.Path destination) throws java.io.IOException
- Parameters:
source
- the source directory as a file, must not be nulldestination
- the destination directory as a file, must not be null- Throws:
java.io.IOException
- if a problem occurs
-
copyDirectory
public static void copyDirectory(java.io.File source, java.io.File destination) throws java.io.IOException
- Parameters:
source
- the source directory as a file, must not be nulldestination
- the destination directory as a file, must not be null- Throws:
java.io.IOException
- if a problem occurs
-
-