Class CFile

    • Constructor Detail

      • CFile

        public CFile​(java.io.File file)
      • CFile

        public CFile​(java.lang.String pathname)
      • CFile

        public CFile​(java.lang.String parent,
                     java.lang.String child)
      • CFile

        public CFile​(java.io.File parent,
                     java.lang.String child)
      • CFile

        public CFile​(java.net.URI uri)
    • Method Detail

      • fromRemote

        public static CFile fromRemote​(java.lang.String ip,
                                       java.io.File parent,
                                       java.lang.String child)
      • fromRemote

        public static CFile fromRemote​(java.lang.String ip,
                                       java.lang.String fullName)
      • fromRemote

        public static CFile fromRemote​(java.lang.String ip,
                                       CFile path)
      • fromURL

        public static CFile fromURL​(java.net.URL url,
                                    java.io.File parent,
                                    java.lang.String child)
      • fromURL

        public static CFile fromURL​(java.net.URL url,
                                    java.lang.String fullName)
      • fromURL

        public static CFile fromURL​(java.net.URL url,
                                    CFile destination)
      • fromCurrent

        public static CFile fromCurrent​(java.lang.String child)
        return a CFile which is pointing to the file in current folder.
        Parameters:
        child - file name to be referred to
        Returns:
        a CFile which is pointing to the child
      • fromOutput

        public static CFile fromOutput​(java.lang.String child)
        return a CFile which is pointing to the file in output folder.
        Parameters:
        child - file name to be referred to
        Returns:
        a CFile which is pointing to the child
      • fromStorage

        public static CFile fromStorage​(java.lang.String child)
        return a CFile which is pointing to the file in current storage.
        Parameters:
        child - file name to be referred to
        Returns:
        a CFile which is pointing to the file
      • fromTmp

        public static CFile fromTmp​(java.lang.String child)
        return a CFile which is pointing to the file in CConfigs.Path.getTempFolder() folder.
        Parameters:
        child - file name to be referred to
        Returns:
        a CFile which is pointing to the file
      • append

        public CFile append​(java.lang.String content)
        Append a text to a file creating the file if it does not exist. The parent directories of the file will be created if they do not exist.
        Parameters:
        content - the content to write to the file
        Returns:
        current instance of CFile
        Throws:
        CFileOperationException - if anything goes wrong
      • append

        public CFile append​(java.lang.String content,
                            java.nio.charset.Charset charset)
        Append a text to a file creating the file if it does not exist. The parent directories of the file will be created if they do not exist.
        Parameters:
        content - the content to write to the file
        charset - the charset to use
        Returns:
        current instance of CFile
        Throws:
        CFileOperationException - if anything goes wrong
      • append

        public CFile append​(byte[] content)
        Append a byte array to a file creating the file if it does not exist. The parent directories of the file will be created if they do not exist.
        Parameters:
        content - the content to write to the file
        Returns:
        current instance of CFile
        Throws:
        CFileOperationException - if anything goes wrong
      • copy

        public CFile copy​(java.io.File destFile)
        Copies a whole directory or file to a new location preserving the file dates.

        If the current file is directory then this method copies the specified directory and all its child directories and files to the specified destination. The destination is the new location and name of the directory.

        Note:

        If the destination directory did exist, then this method merges the source with the destination, with the source taking precedence.

        If the current file is not directory then this method copy it to the destination.

        The destination directory is created if it does not exist.

        This method tries to preserve the files' last modified date/times using File.setLastModified(long), however it is not guaranteed that those operations will succeed. If the modification operation fails, no indication is provided.

        Parameters:
        destFile - where file or directory should be copy
        Returns:
        current instance
      • copyFile

        public CFile copyFile​(java.io.File destFile)
        Copies a file to a new location preserving the file date.

        This method copies the contents of the specified source file to the specified destination file. The directory holding the destination file is created if it does not exist. If the destination file exists, then this method will overwrite it.

        Note: This method tries to preserve the file's last modified date/times using File.setLastModified(long), however it is not guaranteed that the operation will succeed. If the modification operation fails, no indication is provided.

        Parameters:
        destFile - the new file, must not be null
        Returns:
        current instance of CFile
        Throws:
        CFileOperationException - if something goes wrong
      • createNewFile

        public boolean createNewFile()
        Atomically creates a new, empty file named by this abstract pathname if and only if a file with this name does not yet exist. The check for the existence of the file and the creation of the file if it does not exist are a single operation that is atomic with respect to all other filesystem activities that might affect the file.

        Note: this method should not be used for file-locking, as the resulting protocol cannot be made to work reliably. The FileLock facility should be used instead.

        Overrides:
        createNewFile in class java.io.File
        Returns:
        true if the named file does not exist and was successfully created; false if the named file already exists
      • delete

        public boolean delete()
        Deletes the file or directory denoted by this abstract pathname. If this pathname denotes a directory, then the directory will be clean first.
        Overrides:
        delete in class java.io.File
        Returns:
        true if and only if the file or directory is successfully deleted; false otherwise
      • forceDelete

        public CFile forceDelete()
        Deletes a file. If file is a directory, delete it and all sub-directories.

        The difference between delete() and this method are:

        • A directory to be deleted does not have to be empty.
        • You get exceptions when a file or directory cannot be deleted.
        Returns:
        current instance of CFile
        Throws:
        CFileOperationException - if anything goes wrong
      • forceMkdirs

        public boolean forceMkdirs()
        Delete the file or directory then if the current is directory then create an empty one; if it is file then do nothing.
        Returns:
        true if the directory been created otherwise false
      • getCanonicalFile

        public CFile getCanonicalFile()
        Returns the canonical form of this abstract pathname. Equivalent to new File(this.getCanonicalPath()).
        Overrides:
        getCanonicalFile in class java.io.File
        Returns:
        The canonical pathname string denoting the same file or directory as this abstract pathname
      • getCanonicalPath

        public java.lang.String getCanonicalPath()
        Returns the canonical pathname string of this abstract pathname.

        Every pathname that denotes an existing file or directory has a unique canonical form. Every pathname that denotes a nonexistent file or directory also has a unique canonical form. The canonical form of the pathname of a nonexistent file or directory may be different from the canonical form of the same pathname after the file or directory is created. Similarly, the canonical form of the pathname of an existing file or directory may be different from the canonical form of the same pathname after the file or directory is deleted.

        Overrides:
        getCanonicalPath in class java.io.File
        Returns:
        The canonical pathname string denoting the same file or directory as this abstract pathname
      • getChildFile

        public CFile getChildFile​(java.lang.String filename)
        Returns the CFile which refer to the abstract pathname's child file.
        Parameters:
        filename - the path to the child file
        Returns:
        the CFile which refer to the abstract pathname's child file.
      • getChildFolder

        public CFile getChildFolder​(java.lang.String filename)
        Returns the CFile which refer to the abstract pathname's child folder.
        Parameters:
        filename - the path to the child folder
        Returns:
        the CFile which refer to the abstract pathname's child folder.
      • getInputStream

        public java.io.InputStream getInputStream()
        Get the Input Stream for the file
        Returns:
        the Input Stream for the file
      • getParentFile

        public CFile getParentFile()
        Returns the CFile which refer to the abstract pathname's parent, or null if current file does not name a parent directory.

        The parent of an abstract pathname consists of the pathname's prefix, if any, and each name in the pathname's name sequence except for the last. If the name sequence is empty then the pathname does not name a parent directory.

        Overrides:
        getParentFile in class java.io.File
        Returns:
        The abstract pathname of the parent directory named by this abstract pathname, or null if this pathname does not name a parent
      • moveTo

        public CFile moveTo​(java.io.File dest)
        Move file to new destination and return the CFile: which points to new file.
        Parameters:
        dest - where the file should be move to
        Returns:
        the CFile: which points to new file
      • readLines

        public CList<java.lang.String> readLines()
        Read all lines from a file. This method ensures that the file is closed when all bytes have been read or an I/O error, or other runtime exception, is thrown. Bytes from the file are decoded into characters using the specified charset.

        This method recognizes the following as line terminators:

        • \u000D followed by \u000A, CARRIAGE RETURN followed by LINE FEED
        • \u000A, LINE FEED
        • \u000D, CARRIAGE RETURN

        Additional Unicode line terminators may be recognized in future releases.

        Note that this method is intended for simple cases where it is convenient to read all lines in a single operation. It is not intended for reading in large files.

        Returns:
        the lines from the file as a CList; whether the CList is modifiable or not is implementation dependent and therefore not specified
        Throws:
        CFileOperationException - if anything goes wrong
        See Also:
        contains all lines from the file
      • readLines

        public CList<java.lang.String> readLines​(java.nio.charset.Charset charset)
        Read all lines from a file. This method ensures that the file is closed when all bytes have been read or an I/O error, or other runtime exception, is thrown. Bytes from the file are decoded into characters using the specified charset.

        This method recognizes the following as line terminators:

        • \u000D followed by \u000A, CARRIAGE RETURN followed by LINE FEED
        • \u000A, LINE FEED
        • \u000D, CARRIAGE RETURN

        Additional Unicode line terminators may be recognized in future releases.

        Parameters:
        charset - the charset to use for decoding
        Returns:
        the lines from the file as a List; whether the List is modifiable or not is implementation dependent and therefore not specified
        Throws:
        CFileOperationException - if anything goes wrong
        See Also:
        contains all lines from the file
      • readString

        public java.lang.String readString()
        Reads all characters from a file into a string, decoding from bytes to characters using default charset. The method ensures that the file is closed when all content have been read or an I/O error, or other runtime exception, is thrown.

        This method reads all content including the line separators in the middle and/or at the end. The resulting string will contain line separators as they appear in the file.

        Returns:
        a String containing the content read from the file
        Throws:
        CFileOperationException - if anything goes wrong
      • readString

        public java.lang.String readString​(java.nio.charset.Charset charset)
        Reads all characters from a file into a string, decoding from bytes to characters using the specified charset. The method ensures that the file is closed when all content have been read or an I/O error, or other runtime exception, is thrown.

        This method reads all content including the line separators in the middle and/or at the end. The resulting string will contain line separators as they appear in the file.

        Parameters:
        charset - the charset to use for decoding
        Returns:
        a String containing the content read from the file
        Throws:
        CFileOperationException - if anything goes wrong
      • safeRename

        public CFile safeRename​(java.io.File destFile,
                                boolean overwrite)
        If the destination file exist and the overwrite parameter is false then do nothing. If the destination file exist and the overwrite parameter is true then delete the destination file. After that get File.renameTo(File) and then delete the original file
        Parameters:
        destFile - where the file or directory should copy to
        overwrite - whether we should overwrite the destination if it is exists
        Returns:
        CFile pointing to the destination
      • waitForWritable

        public boolean waitForWritable​(int timeoutInSeconds)
        Wait for timeoutInSeconds for file to be available and writable
        Parameters:
        timeoutInSeconds - timeout in seconds to wait for file to be available
        Returns:
        true if file is available and writable otherwise false
      • waitForWritable

        public boolean waitForWritable​(int timeoutInSeconds,
                                       int intervalInMillSeconds)
        Wait for timeoutInSeconds for file to be available and writable
        Parameters:
        timeoutInSeconds - timeout in seconds to wait for file to be available
        intervalInMillSeconds - the interval of milliseconds between each retry
        Returns:
        true if file is available and writable otherwise false
      • waitToExists

        public boolean waitToExists​(int timeoutInSeconds)
        Wait for timeoutInSeconds for file to be available
        Parameters:
        timeoutInSeconds - timeout in seconds to wait for file to be available
        Returns:
        true if file is available otherwise false
      • waitToExists

        public boolean waitToExists​(int timeoutInSeconds,
                                    int intervalInMillSeconds)
        Wait for timeoutInSeconds for file to be available
        Parameters:
        timeoutInSeconds - timeout in seconds to wait for file to be available
        intervalInMillSeconds - the interval of milliseconds between each retry
        Returns:
        true if file is available otherwise false
      • writeJson

        public CFile writeJson​(java.lang.Object object)
        Writes an object as json to a file creating the file if it does not exist. The parent directories of the file will be created if they do not exist.
        Parameters:
        object - the object to be serialize and write to the file
        Returns:
        current instance of CFile
        Throws:
        CFileOperationException - if anything goes wrong
      • writeJson

        public CFile writeJson​(java.lang.Object object,
                               java.nio.charset.Charset charset)
        Writes an object as json to a file creating the file if it does not exist. The parent directories of the file will be created if they do not exist.
        Parameters:
        object - the object to be serialize and write to the file
        charset - the charset to use
        Returns:
        current instance of CFile
        Throws:
        CFileOperationException - if anything goes wrong
      • write

        public CFile write​(java.lang.String content)
        Writes a text to a file creating the file if it does not exist. The parent directories of the file will be created if they do not exist.
        Parameters:
        content - the content to write to the file
        Returns:
        current instance of CFile
        Throws:
        CFileOperationException - if anything goes wrong
      • write

        public CFile write​(java.lang.String content,
                           java.nio.charset.Charset charset)
        Writes a text to a file creating the file if it does not exist. The parent directories of the file will be created if they do not exist.
        Parameters:
        content - the content to write to the file
        charset - the charset to use
        Returns:
        current instance of CFile
        Throws:
        CFileOperationException - if anything goes wrong
      • write

        public CFile write​(byte[] content)
        Writes a byte array to a file creating the file if it does not exist. The parent directories of the file will be created if they do not exist.
        Parameters:
        content - the content to write to the file
        Returns:
        current instance of CFile
        Throws:
        CFileOperationException - if anything goes wrong
      • write

        public CFile write​(java.io.InputStream inputStream)
        Writes a byte array to a file creating the file if it does not exist. The parent directories of the file will be created if they do not exist.
        Parameters:
        inputStream - the input stream
        Returns:
        current instance of CFile
        Throws:
        CFileOperationException - if anything goes wrong
      • toByteArray

        public byte[] toByteArray()
        Convert file to byte array and return the result.
        Returns:
        file data in byte[] format
        Throws:
        CFileOperationException - for any IOException
      • notExist

        public boolean notExist()
        Check whether the file or directory denoted by this abstract pathname exists.
        Returns:
        true if and only if the file or directory denoted by this abstract pathname does not exist; false otherwise
        Throws:
        java.lang.SecurityException - If a security manager exists and its SecurityManager.checkRead(java.lang.String) method denies read access to the file or directory
      • copyToRemoteFolder

        public CFile copyToRemoteFolder​(java.lang.String remoteIP,
                                        java.io.File destFolder)
      • copyFromRemoteFolder

        public CFile copyFromRemoteFolder​(java.lang.String remoteIP,
                                          java.io.File destFolder)
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.io.File
      • getValue

        public CFile getValue()
        Description copied from interface: CBaseState
        For internal use only
        Specified by:
        getValue in interface CBaseState<java.io.File>