Class FileServices

java.lang.Object
com.ebasetech.xi.services.FileServices
All Implemented Interfaces:
java.io.Serializable

public class FileServices
extends java.lang.Object
implements java.io.Serializable
File services File path parameters can be either absolute or relative.
Relative paths are resolved against the user data application directory. e.g. userdataDir/apps/ebase
Since:
V4.4
See Also:
Serialized Form
  • Constructor Summary

    Constructors
    Constructor Description
    FileServices()  
  • Method Summary

    Modifier and Type Method Description
    static void appendToFile​(java.lang.String filePath, java.lang.String text)
    Creates a new file if necessary with the specified file path and appends the specified text to the file.
    static void appendToFile​(java.lang.String filePath, java.lang.String text, java.lang.String encoding)
    Creates a new file if necessary with the specified file path and appends the specified text decoded using the specified encoding to the file.
    static byte[] base64Decode​(java.lang.String encoded)
    Returns decoded text from a base64 string.
    static java.lang.String base64Encode​(byte[] bytes)
    Returns the text as a Base64 encoded string
    static java.lang.String base64EncodeFile​(java.lang.String filename)
    Returns the text as a Base64 encoded string
    static boolean copyFile​(java.lang.String fromPath, java.lang.String toPath)
    Copies the contents of the file represented by fromPath to toPath.
    static boolean createNewDirectory​(java.lang.String filePath)
    Creates a new directory with this path name including any necessary but nonexistent parent directories.
    static boolean createNewFile​(java.lang.String filePath)
    Creates a new file with this path name so long as a file or directory doesn't already exist with the same name.
    static boolean deleteFile​(java.lang.String filePath)
    Deletes the file or directory with this path name.
    static boolean existsFile​(java.lang.String filePath)
    Returns true if a file or directory with this path name exists.
    static java.lang.String getAbsoluteFilePath​(java.lang.String filePath)
    Returns an absolute file path for the specified relative or absolute file path.
    static java.lang.String getParentPath​(java.lang.String filePath)
    Returns the path name of this file's parent directory or null if no parent exists.
    static java.lang.String getSeparator()
    Returns the system dependent file separator character.
    static java.lang.String getUserDataApplicationPath()
    Returns the file path for this application within the user data file system.
    static java.lang.String getWebAppRootFilePath()
    Returns the file path for the root directory served by Http requests for the web application.
    static boolean isDirectory​(java.lang.String filePath)
    Returns true if file path represents a directory.
    static boolean isFile​(java.lang.String filePath)
    Returns true if file path represents a file.
    static java.lang.String[] listFilesInDirectory​(java.lang.String filePath)
    Returns an array of file paths for all files and directories within the directory represented by file path.
    static boolean moveFile​(java.lang.String fromPath, java.lang.String toPath)
    Moves the file specified by fromPath to the path specified by toPath.
    static java.lang.String readFile​(java.lang.String filePath)
    Returns a String value representing the contents of the file specified by file path.
    static java.lang.String readFile​(java.lang.String filePath, java.lang.String encoding)
    Returns a String value representing the contents of the file specified by file path decoded using the specified encoding.
    static void writeFile​(java.lang.String filePath, java.lang.String text)
    Creates a new file if necessary with the specified file path and saves the file with the specified text.
    static void writeFile​(java.lang.String filePath, java.lang.String text, java.lang.String encoding)
    Creates a new file if necessary with the specified file path and saves the file with the specified text decoded using the specified encoding.

    Methods inherited from class java.lang.Object

    equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • FileServices

      public FileServices()
  • Method Details

    • createNewDirectory

      public static boolean createNewDirectory​(java.lang.String filePath) throws java.io.IOException
      Creates a new directory with this path name including any necessary but nonexistent parent directories.
      Parameters:
      filePath - for the new directory. see above
      Returns:
      true if the new directory was created, false if the file name already exists
      Throws:
      java.io.IOException - if an error occurs
      Since:
      V4.4
    • createNewFile

      public static boolean createNewFile​(java.lang.String filePath) throws java.io.IOException
      Creates a new file with this path name so long as a file or directory doesn't already exist with the same name.
      Parameters:
      filePath - file path for the new file. see above
      Returns:
      true if the new file was created, false if the file name already exists
      Throws:
      java.io.IOException - if an error occurs
      Since:
      V4.4
    • deleteFile

      public static boolean deleteFile​(java.lang.String filePath) throws java.io.IOException
      Deletes the file or directory with this path name. If the path specifies a directory, the directory must be empty.
      Parameters:
      filePath - to be deleted. see above
      Returns:
      true if the file or directory was deleted, false otherwise
      Throws:
      java.io.IOException - if an error occurs
      Since:
      V4.4
    • existsFile

      public static boolean existsFile​(java.lang.String filePath)
      Returns true if a file or directory with this path name exists.
      Parameters:
      filePath - see above
      Returns:
      true if a file or directory with this path name exists, false otherwise
      Since:
      V4.4
    • getParentPath

      public static java.lang.String getParentPath​(java.lang.String filePath)
      Returns the path name of this file's parent directory or null if no parent exists.
      Parameters:
      filePath - see above
      Returns:
      path of parent directory or null if no parent exists
      Since:
      V4.4
    • isDirectory

      public static boolean isDirectory​(java.lang.String filePath)
      Returns true if file path represents a directory.
      Parameters:
      filePath - see above
      Returns:
      true if the file path represents a directory, false otherwise
      Since:
      V4.4
    • isFile

      public static boolean isFile​(java.lang.String filePath)
      Returns true if file path represents a file.
      Parameters:
      filePath - see above
      Returns:
      true if the file path represents a file, false otherwise
      Since:
      V4.4
    • listFilesInDirectory

      public static java.lang.String[] listFilesInDirectory​(java.lang.String filePath)
      Returns an array of file paths for all files and directories within the directory represented by file path.
      Parameters:
      filePath - see above
      Returns:
      an array of file paths, or null if file path does not specify a directory
      Since:
      V4.4
    • readFile

      public static java.lang.String readFile​(java.lang.String filePath) throws java.io.IOException
      Returns a String value representing the contents of the file specified by file path.
      Parameters:
      filePath - see above
      Returns:
      file contents
      Throws:
      java.io.IOException - if an error occurs
      Since:
      V4.4
    • readFile

      public static java.lang.String readFile​(java.lang.String filePath, java.lang.String encoding) throws java.io.IOException
      Returns a String value representing the contents of the file specified by file path decoded using the specified encoding.
      Parameters:
      filePath - see above
      encoding - character set name
      Returns:
      file contents
      Throws:
      java.io.IOException - if an error occurs
      Since:
      V4.4
    • moveFile

      public static boolean moveFile​(java.lang.String fromPath, java.lang.String toPath) throws java.io.IOException
      Moves the file specified by fromPath to the path specified by toPath. Returns false if either fromPath does not exist or toPath does exist. This operation is equivalent to a rename.
      Parameters:
      fromPath - the file to be renamed. see above
      toPath - the new name. see above
      Returns:
      true if the operation was succesful, false otherwise
      Throws:
      java.io.IOException - if an error occurs
      Since:
      V4.4
    • appendToFile

      public static void appendToFile​(java.lang.String filePath, java.lang.String text) throws java.io.IOException
      Creates a new file if necessary with the specified file path and appends the specified text to the file.
      Parameters:
      filePath - see above
      text - text to be append to the file
      Throws:
      java.io.IOException - if an error occurs
      Since:
      V5.3
    • appendToFile

      public static void appendToFile​(java.lang.String filePath, java.lang.String text, java.lang.String encoding) throws java.io.IOException
      Creates a new file if necessary with the specified file path and appends the specified text decoded using the specified encoding to the file.
      Parameters:
      filePath - see above
      text - text to be append to the file
      encoding - character set name
      Throws:
      java.io.IOException - if an error occurs
      Since:
      V5.3
    • writeFile

      public static void writeFile​(java.lang.String filePath, java.lang.String text) throws java.io.IOException
      Creates a new file if necessary with the specified file path and saves the file with the specified text. If the file already exists, it's contents are replaced.
      Parameters:
      filePath - see above
      text - text to be saved
      Throws:
      java.io.IOException - if an error occurs
      Since:
      V4.4
    • writeFile

      public static void writeFile​(java.lang.String filePath, java.lang.String text, java.lang.String encoding) throws java.io.IOException
      Creates a new file if necessary with the specified file path and saves the file with the specified text decoded using the specified encoding. If the file already exists, it's contents are replaced.
      Parameters:
      filePath - see above
      text - text to be saved
      encoding - character set name
      Throws:
      java.io.IOException - if an error occurs
      Since:
      V4.4
    • copyFile

      public static boolean copyFile​(java.lang.String fromPath, java.lang.String toPath) throws java.io.IOException
      Copies the contents of the file represented by fromPath to toPath. Creates a new file for toPath if a file does not already exist. Returns false if the file represented by fromPath does not exist.
      Parameters:
      fromPath - the file to be copied. see above
      toPath - the target file. see above
      Returns:
      true if the operation was successful, false if the file represented by fromPath does not exist
      Throws:
      java.io.IOException - if an error occurs
      Since:
      V4.4
    • getSeparator

      public static java.lang.String getSeparator()
      Returns the system dependent file separator character. For Unix and Linux systems this will be '/', for Windows systems it will be '\'.
      Returns:
      file separator character
      Since:
      V4.4
    • getWebAppRootFilePath

      public static java.lang.String getWebAppRootFilePath()
      Returns the file path for the root directory served by Http requests for the web application.
      Returns:
      file path
      Since:
      V4.4
    • getUserDataApplicationPath

      public static java.lang.String getUserDataApplicationPath()
      Returns the file path for this application within the user data file system. For an external server this will resolve to path <userdata>/apps<webappname>.
      Returns:
      file path
      Since:
      V5.7
    • getAbsoluteFilePath

      public static java.lang.String getAbsoluteFilePath​(java.lang.String filePath) throws java.io.IOException
      Returns an absolute file path for the specified relative or absolute file path. For example '/usr/myproduct/deployed/xyz.txt' might be returned for '../../xyz.txt'.
      Parameters:
      filePath - see above
      Returns:
      an absolute file path
      Throws:
      java.io.IOException
      Since:
      V4.4
    • base64EncodeFile

      public static final java.lang.String base64EncodeFile​(java.lang.String filename) throws java.io.IOException
      Returns the text as a Base64 encoded string
      Parameters:
      filename - to encode. see above
      Throws:
      java.io.IOException
      Since:
      V5.2
    • base64Encode

      public static final java.lang.String base64Encode​(byte[] bytes)
      Returns the text as a Base64 encoded string
      Parameters:
      bytes - to encode
      Since:
      V5.2
    • base64Decode

      public static final byte[] base64Decode​(java.lang.String encoded)
      Returns decoded text from a base64 string.
      Parameters:
      encoded - text to decode
      Since:
      V5.2