Class JKIOUtil

java.lang.Object
com.jk.core.util.JKIOUtil

public class JKIOUtil extends Object
This class is a utility class used for various I/O operations and file handling tasks.
Version:
1.0
Author:
Dr. Jalal H. Kiswani
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final String
    Represents the new line separator.
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static File
    checkFolderPath(String path, boolean create)
    This method checks the specified folder path and optionally creates it if it does not exist.
    static void
    This method clears temporary files from the user's local folder.
    static void
    close(InputStream inputStream)
    This method closes the provided input stream.
    static String
    This method converts the content of an InputStream to a string using UTF-8 encoding.
    static void
    copResourcesFromJarToDir(String sourceClassPath, File dest)
    This method copies resources from a specified class path location within a JAR file to a destination directory on the local file system.
    static void
    createDirectory(File parent, String name)
    This method creates a directory with the specified name within the specified parent directory.
    static void
    This method creates a directory with the specified path.
    static void
    createDirectory(String parent, String name)
    This method creates a directory with the specified name within the specified parent directory.
    static File
    This method creates a temporary directory with a unique name in the default temporary directory of the operating system.
    static File
    This method creates a temporary file with the given file extension.
    static boolean
    This method recursively deletes a directory and all its contents.
    static void
    deleteDir(String fullPath)
    This method deletes the directory and all its contents at the specified full path.
    static void
    deleteFile(String templateFile)
    This method deletes the specified file.
    static void
    This method deletes all files within the specified directory.
    static Process
    executeFile(String fileName)
    This method executes the specified file using the default system application associated with the file type.
    static void
    This method executes the specified command in the context of the given root directory and waits for the command to complete.
    static String
    This method finds the absolute path of the JAR file containing the provided class.
    static List<String>
    This method retrieves a list of directory names within a specific folder.
    static String
    getExtension(String fileName, boolean withPoint)
    This method retrieves the extension of the given file name.
    static String
    This method retrieves the file separator.
    static List<String>
    This method retrieves a list of file names within a specific folder that match the given file extension.
    This method retrieves an InputStream for a resource with the given name.
    static String
    This method retrieve the local host name.
    static Reader
    This method retrieves a Reader for the specified resource name.
    This method retrieves an input stream for a report file with the specified name.
    static String
    getReportsOutPath(boolean appendFileSeprator)
    This method retrieves the path for storing generated reports, either with or without appending a file separator at the end.
    static List<File>
    This method recursively retrieves a list of files from a specified package location within the class path.
    static String
    getSqlFile(String fileName)
    This method retrieves the content of an SQL file located in the specified path within the class path.
    static List<String>
    This method retrieves a list of names of sub-directories within the specified directory.
    static URL
    getURL(String path)
    This method retrieves a URL object for the specified resource path.
    static String
    getUserFolderPath(boolean appendFileSeprator)
    This method gets the user's local folder path where application-related data can be stored.
    static boolean
    isEmpty(String sqlFilePath)
    This method is yet to be implemented (TBI).
    static boolean
    isFileExist(String fileName)
    This method checks whether a file with the specified name exists.
    static boolean
    This method checks whether the platform is Windows or not.
    static String
    r(String string)
    ...
    static String
    readFile(File file)
    This method reads the contents of a file and returns them as a string.
    static String
    readFile(String string)
    This method reads the content of a file specified by the given file name.
    static byte[]
    This method reads the content of a file as a byte array.
    static <T> T
    readFileAsYaml(String file, Class<T> clas)
    This method reads the content of a YAML file and converts it to an object of the specified class.
    static Properties
    This method reads a properties file from the specified file.
    static Properties
    This method reads and loads properties from a properties file located at the specified path.
    static Properties
    This method reads the contents of an input stream as properties and returns them as a Properties object.
    static byte[]
    This method reads the content of an InputStream and returns it as a byte array.
    static String
    This method reads the content of an InputStream and returns it as a string.
    static String
    This method removes the extension from the given file name.
    static void
    renameDir(String source, String dist)
    This method renames a directory from the source path to the destination path.
    static File
    renameFile(File file, String targetName)
    This method renames a file to the specified target name.
    static void
    replaceValueInFile(String fileName, String sourceValue, String targetValue)
    This method replaces all occurrences of a source value with a target value in the specified file.
    static void
    This method starts a fake thread that keeps running indefinitely, preventing the provided ServerSocket from being garbage-collected and closed.
    static void
    writeBytesToFile(byte[] data, String localFile)
    This method writes a byte array to a specified local file.
    static File
    writeDataToFile(byte[] data, File file)
    This method writes the provided byte array data to the specified file.
    static File
    writeDataToFile(byte[] data, File file, boolean append)
    This method writes the provided byte array data to the specified file.
    static void
    This method writes the provided string data to the specified file.
    static void
    This method writes the specified text data to the specified file.
    static File
    writeDataToTempFile(byte[] data, String suffix)
    This method writes the provided byte array data to a temporary file with the specified suffix.
    static File
    This method writes the provided data as a string to a temporary file with the given file extension.
    static File
    writeFileToTempDirectory(byte[] data, String fileName)
    This method writes the provided byte array data to a temporary file with the specified file name and places it in the user's temporary directory.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • NEW_LINE

      public static final String NEW_LINE
      Represents the new line separator.
  • Constructor Details

    • JKIOUtil

      public JKIOUtil()
  • Method Details

    • findPathJar

      public static String findPathJar(Class clas)
      This method finds the absolute path of the JAR file containing the provided class.

      If the provided class has been loaded from a jar file that is on the local file system, will find the absolute path to that jar file.

      Parameters:
      clas - Specifies the class to find the JAR file for.
      Returns:
      the absolute path to the JAR file.
      Throws:
      IllegalStateException - If the specified class was loaded from a directory or in some other way (such as via HTTP, from a database, or some other custom class loading device).
    • getInputStream

      public static InputStream getInputStream(String name)
      This method retrieves an InputStream for a resource with the given name.
      Parameters:
      name - Specifies the name of the resource.
      Returns:
      the InputStream for the resource.
    • readPropertiesFile

      public static Properties readPropertiesFile(File file)
      This method reads a properties file from the specified file.
      Parameters:
      file - Specifies the File object representing the properties file to be read.
      Returns:
      the Properties object containing the properties read from the file, or an empty Properties object if the file does not exist.
    • readStream

      public static byte[] readStream(InputStream inStream)
      This method reads the content of an InputStream and returns it as a byte array.
      Parameters:
      inStream - Specifies the InputStream to read from.
      Returns:
      the byte array containing the content read from the InputStream.
    • readStreamAsString

      public static String readStreamAsString(InputStream inStream)
      This method reads the content of an InputStream and returns it as a string.
      Parameters:
      inStream - Specifies the InputStream to read from.
      Returns:
      the string containing the content read from the InputStream.
    • convertToString

      public static String convertToString(InputStream input)
      This method converts the content of an InputStream to a string using UTF-8 encoding.
      Parameters:
      input - Specifies the InputStream to convert.
      Returns:
      the string representation of the content read from the InputStream.
    • getUserFolderPath

      public static String getUserFolderPath(boolean appendFileSeprator)
      This method gets the user's local folder path where application-related data can be stored.
      Parameters:
      appendFileSeprator - Indicated whether to append a file separator at the end of the path.
      Returns:
      the user's local folder path.
    • checkFolderPath

      public static File checkFolderPath(String path, boolean create)
      This method checks the specified folder path and optionally creates it if it does not exist.
      Parameters:
      path - Specifies the folder path to check.
      create - Indicated whether to create the folder if it doesn't exist.
      Returns:
      the File object representing the checked folder path, or null if the folder doesn't exist and creation is not requested.
    • getReader

      public static Reader getReader(String name)
      This method retrieves a Reader for the specified resource name.
      Parameters:
      name - Specifies the name of the resource to retrieve as a Reader.
      Returns:
      the Reader for the specified resource, or null if the resource is not found.
    • readFile

      public static String readFile(String string)
      This method reads the content of a file specified by the given file name.
      Parameters:
      string - Specifies the name of the file to read.
      Returns:
      the content of the file as a string, or null if the file is not found or an error occurs.
    • getExtension

      public static String getExtension(String fileName, boolean withPoint)
      This method retrieves the extension of the given file name.
      Parameters:
      fileName - Specifies the name of the file from which to extract the extension.
      withPoint - Indicates whether to include the period ('.') in the extension. If true, the extension will include the period; if false, it won't.
      Returns:
      the file extension with or without the period, based on the 'withPoint' parameter.
    • removeExtension

      public static String removeExtension(String fileName)
      This method removes the extension from the given file name.
      Parameters:
      fileName - Specifies the name of the file from which to remove the extension.
      Returns:
      the file name without the extension.
    • writeDataToFile

      public static File writeDataToFile(byte[] data, File file)
      This method writes the provided byte array data to the specified file.
      Parameters:
      data - Specifies the byte array data to write to the file.
      file - Specifies the file to which the data will be written.
      Returns:
      the file to which the data was written.
    • writeDataToFile

      public static File writeDataToFile(byte[] data, File file, boolean append)
      This method writes the provided byte array data to the specified file. The data can be appended to the file if the 'append' parameter is set to true.
      Parameters:
      data - Specifies the byte array data to write to the file.
      file - Specifies the file to which the data will be written.
      append - Indicated whether the data will be appended to the file (true), or the existing content will be replaced (false).
      Returns:
      the file to which the data was written.
    • readPropertiesStream

      public static Properties readPropertiesStream(InputStream inputStream)
      This method reads the contents of an input stream as properties and returns them as a Properties object.
      Parameters:
      inputStream - Specifies the input stream containing properties data.
      Returns:
      the Properties object containing the properties read from the input stream.
    • close

      public static void close(InputStream inputStream)
      This method closes the provided input stream. If the input stream is not null, it will be closed.
      Parameters:
      inputStream - Specifies the input stream to be closed.
    • writeDataToTempFile

      public static File writeDataToTempFile(byte[] data, String suffix)
      This method writes the provided byte array data to a temporary file with the specified suffix.
      Parameters:
      data - Specifies the byte array data to be written to the file.
      suffix - Specifies the suffix to be used for the temporary file.
      Returns:
      the File object representing the created temporary file containing the data.
    • writeFileToTempDirectory

      public static File writeFileToTempDirectory(byte[] data, String fileName)
      This method writes the provided byte array data to a temporary file with the specified file name and places it in the user's temporary directory.
      Parameters:
      data - Specifies the byte array data to be written to the file.
      fileName - Specifies the name of the temporary file to be created.
      Returns:
      the File object representing the created temporary file containing the data.
    • createTempDirectory

      public static File createTempDirectory()
      This method creates a temporary directory with a unique name in the default temporary directory of the operating system.
      Returns:
      the File object representing the newly created temporary directory.
    • writeDataToTempFile

      public static File writeDataToTempFile(String data, String ext)
      This method writes the provided data as a string to a temporary file with the given file extension.
      Parameters:
      data - Specifies the data to be written to the temporary file.
      ext - Specifies the file extension to be used for the temporary file.
      Returns:
      the File object representing the newly created temporary file.
    • createTempFile

      public static File createTempFile(String ext)
      This method creates a temporary file with the given file extension.
      Parameters:
      ext - Specifies the file extension to be used for the temporary file.
      Returns:
      the File object representing the newly created temporary file.
    • getURL

      public static URL getURL(String path)
      This method retrieves a URL object for the specified resource path.
      Parameters:
      path - Specifies the path to the resource for which the URL is to be retrieved.
      Returns:
      the URL object representing the resource, or null if the resource is not found.
    • readPropertiesFile

      public static Properties readPropertiesFile(String fileName)
      This method reads and loads properties from a properties file located at the specified path.
      Parameters:
      fileName - Specifies the name or path of the properties file to be read.
      Returns:
      the Properties object containing the loaded properties, or an empty Properties object if the file is not found.
    • getReportFileAsStream

      public static InputStream getReportFileAsStream(String name)
      This method retrieves an input stream for a report file with the specified name.
      Parameters:
      name - Specifies the name of the report file to retrieve as an input stream.
      Returns:
      the InputStream providing access to the content of the report file, or null if the file is not found.
    • executeFile

      public static Process executeFile(String fileName)
      This method executes the specified file using the default system application associated with the file type.

      This method is platform-dependent and supports execution of files on Windows systems.

      Parameters:
      fileName - Specifies the name of the file to be executed.
      Returns:
      the Process representing the execution of the file, or null if execution fails or is not supported on the platform.
    • getSqlFile

      public static String getSqlFile(String fileName)
      This method retrieves the content of an SQL file located in the specified path within the class path.
      Parameters:
      fileName - Specifies the name of the SQL file to retrieve, relative to the /jk/sql/ directory within the class path.
      Returns:
      the content of the SQL file as a string, or null if the file is not found.
    • getLocalHostName

      public static String getLocalHostName()
      This method retrieve the local host name.
      Returns:
      the local host name.
    • readFileAsByteArray

      public static byte[] readFileAsByteArray(String fileName)
      This method reads the content of a file as a byte array.
      Parameters:
      fileName - Specifies the name of the file to read.
      Returns:
      the content of the file as a byte array, or null if the file is not found or an error occurs.
    • writeBytesToFile

      public static void writeBytesToFile(byte[] data, String localFile)
      This method writes a byte array to a specified local file.
      Parameters:
      data - Specifies the byte array to write to the file.
      localFile - Specifies the path of the local file to write the byte array to.
    • isWindows

      public static boolean isWindows()
      This method checks whether the platform is Windows or not.
      Returns:
      true, if it is Windows, false otherwise.
    • readFile

      public static String readFile(File file)
      This method reads the contents of a file and returns them as a string.
      Parameters:
      file - Specifies the file to read.
      Returns:
      the contents of the file as a string.
    • isFileExist

      public static boolean isFileExist(String fileName)
      This method checks whether a file with the specified name exists.
      Parameters:
      fileName - Specifies the name of the file to check.
      Returns:
      true, if a file with the specified name exists, false otherwise.
    • createDirectory

      public static void createDirectory(String path)
      This method creates a directory with the specified path.
      Parameters:
      path - Specifies the path of the directory to be created.
    • isEmpty

      public static boolean isEmpty(String sqlFilePath)
      This method is yet to be implemented (TBI).
      Parameters:
      sqlFilePath - Specifies the method parameter (TBI).
      Returns:
      false.
    • clearTempFiles

      public static void clearTempFiles()
      This method clears temporary files from the user's local folder.

      This method deletes all temporary files and directories created by the application in the user's local folder.

    • deleteDir

      public static boolean deleteDir(File dir)
      This method recursively deletes a directory and all its contents.
      Parameters:
      dir - Specifies the directory to be deleted.
      Returns:
      true, if the directory and its contents were successfully deleted, false if there was an issue during deletion.
    • startFakeThread

      public static void startFakeThread(ServerSocket server)
      This method starts a fake thread that keeps running indefinitely, preventing the provided ServerSocket from being garbage-collected and closed.

      This is useful in situations where you want to keep a ServerSocket alive in memory while your application is running.

      Parameters:
      server - Specifies the ServerSocket instance to keep alive.
    • getReportsOutPath

      public static String getReportsOutPath(boolean appendFileSeprator)
      This method retrieves the path for storing generated reports, either with or without appending a file separator at the end.
      Parameters:
      appendFileSeprator - Indicates whether to append a file separator at the end of the path.
      Returns:
      the path for storing generated reports, optionally with an appended file separator.
    • getFileSeparator

      public static String getFileSeparator()
      This method retrieves the file separator.
      Returns:
      the file separator.
    • getFilesInFolder

      public static List<String> getFilesInFolder(String folder, String ext)
      This method retrieves a list of file names within a specific folder that match the given file extension.
      Parameters:
      folder - Specifies the path of the folder in which to search for files.
      ext - Specifies the file extension to filter files by.
      Returns:
      the list of file names within the folder that match the given extension.
    • getDirsInFolder

      public static List<String> getDirsInFolder(String folder)
      This method retrieves a list of directory names within a specific folder.
      Parameters:
      folder - Specifies the path of the folder in which to search for directories.
      Returns:
      the list of directory names within the folder.
    • copResourcesFromJarToDir

      public static void copResourcesFromJarToDir(String sourceClassPath, File dest)
      This method copies resources from a specified class path location within a JAR file to a destination directory on the local file system.
      Parameters:
      sourceClassPath - Specifies the class path location of the resources within the JAR file to be copied.
      dest - Specifies the destination directory where the resources will be copied.
    • getResourcesInnPackage

      public static List<File> getResourcesInnPackage(String root)
      This method recursively retrieves a list of files from a specified package location within the class path.
      Parameters:
      root - Specifies the root package location from which to retrieve files.
      Returns:
      the list of File objects representing the files within the specified package location and its sub-packages
    • writeDataToFile

      public static void writeDataToFile(String data, File file)
      This method writes the provided string data to the specified file.
      Parameters:
      data - Specifies the string data to be written to the file.
      file - Specifies the File object representing the file to which the data will be written.
    • exeutCommandAndWait

      public static void exeutCommandAndWait(File root, String command)
      This method executes the specified command in the context of the given root directory and waits for the command to complete.
      Parameters:
      root - Specifies the root directory in which the command will be executed.
      command - Specifies the command to be executed.
    • writeDataToFile

      public static void writeDataToFile(String text, String file)
      This method writes the specified text data to the specified file.
      Parameters:
      text - Specifies the text data to be written to the file.
      file - Specifies the path of the file to which the data will be written.
    • getSubDirectories

      public static List<String> getSubDirectories(File directory)
      This method retrieves a list of names of sub-directories within the specified directory.
      Parameters:
      directory - Specifies the directory for which to retrieve sub-directory names.
      Returns:
      the list of names of sub-directories within the specified directory
    • createDirectory

      public static void createDirectory(String parent, String name)
      This method creates a directory with the specified name within the specified parent directory.
      Parameters:
      parent - Specifies the parent directory within which the new directory will be created.
      name - Specifies the name of the new directory to be created.
    • createDirectory

      public static void createDirectory(File parent, String name)
      This method creates a directory with the specified name within the specified parent directory.
      Parameters:
      parent - Specifies the parent directory within which the new directory will be created.
      name - Specifies the name of the new directory to be created.
    • deleteDir

      public static void deleteDir(String fullPath)
      This method deletes the directory and all its contents at the specified full path.
      Parameters:
      fullPath - Specifies the full path of the directory to be deleted.
    • replaceValueInFile

      public static void replaceValueInFile(String fileName, String sourceValue, String targetValue)
      This method replaces all occurrences of a source value with a target value in the specified file.
      Parameters:
      fileName - Specifies the name of the file in which replacements will be performed.
      sourceValue - Specifies the value to be replaced.
      targetValue - Specifies the value to replace occurrences of the source value.
    • renameDir

      public static void renameDir(String source, String dist)
      This method renames a directory from the source path to the destination path.
      Parameters:
      source - Specifies the path of the directory to be renamed.
      dist - Specifies the new path to which the directory will be renamed.
    • renameFile

      public static File renameFile(File file, String targetName)
      This method renames a file to the specified target name.
      Parameters:
      file - Specifies the file to be renamed.
      targetName - Specifies the new name for the file.
      Returns:
      the renamed file with the new target name.
    • deleteFile

      public static void deleteFile(String templateFile)
      This method deletes the specified file.
      Parameters:
      templateFile - Specifies the file to be deleted.
    • deleteFilesInDir

      public static void deleteFilesInDir(String path)
      This method deletes all files within the specified directory.
      Parameters:
      path - Specifies the path to the directory containing the files to be deleted.
    • readFileAsYaml

      public static <T> T readFileAsYaml(String file, Class<T> clas)
      This method reads the content of a YAML file and converts it to an object of the specified class.
      Type Parameters:
      T - Specifies the type of the object to be created.
      Parameters:
      file - Specifies the path to the YAML file to be read.
      clas - Specifies the class representing the type of object to be created from the YAML content.
      Returns:
      the instance of the specified class populated with data from the YAML file, or null if an error occurs.
    • r

      public static String r(String string)
      ...
      Parameters:
      string - ...
      Returns:
      ...