Class CFile
- java.lang.Object
-
- java.io.File
-
- org.catools.common.io.CFile
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Comparable<java.io.File>
,CBaseState<java.io.File>
,CFileState
,CObjectState<java.io.File>
,CFileExtension
,CBaseVerifier<java.io.File>
,CFileVerifier
,CObjectVerifier<java.io.File,CFileState>
,CBaseWaiter<java.io.File>
,CFileWaiter
,CObjectWaiter<java.io.File>
public class CFile extends java.io.File implements CFileExtension
- See Also:
- Serialized Form
-
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description CFile
append(byte[] content)
Append a byte array to a file creating the file if it does not exist.CFile
append(java.lang.String content)
Append a text to a file creating the file if it does not exist.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.CFile
copy(java.io.File destFile)
Copies a whole directory or file to a new location preserving the file dates.CFile
copyFile(java.io.File destFile)
Copies a file to a new location preserving the file date.CFile
copyFromRemoteFolder(java.lang.String remoteIP, java.io.File destFolder)
CFile
copyToRemoteFolder(java.lang.String remoteIP, java.io.File destFolder)
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.boolean
delete()
Deletes the file or directory denoted by this abstract pathname.CFile
forceDelete()
Deletes a file.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.static CFile
fromCurrent(java.lang.String child)
return a CFile which is pointing to the file in current folder.static CFile
fromOutput(java.lang.String child)
return a CFile which is pointing to the file in output folder.static CFile
fromRemote(java.lang.String ip, java.io.File parent, java.lang.String child)
static CFile
fromRemote(java.lang.String ip, java.lang.String fullName)
static CFile
fromRemote(java.lang.String ip, CFile path)
static CFile
fromStorage(java.lang.String child)
return a CFile which is pointing to the file in current storage.static CFile
fromTmp(java.lang.String child)
return a CFile which is pointing to the file inCConfigs.Path.getTempFolder()
folder.static CFile
fromURL(java.net.URL url, java.io.File parent, java.lang.String child)
static CFile
fromURL(java.net.URL url, java.lang.String fullName)
static CFile
fromURL(java.net.URL url, CFile destination)
CFile
getCanonicalFile()
Returns the canonical form of this abstract pathname.java.lang.String
getCanonicalPath()
Returns the canonical pathname string of this abstract pathname.CFile
getChildFile(java.lang.String filename)
Returns the CFile which refer to the abstract pathname's child file.CFile
getChildFolder(java.lang.String filename)
Returns the CFile which refer to the abstract pathname's child folder.java.io.InputStream
getInputStream()
Get the Input Stream for the fileCFile
getParentFile()
Returns the CFile which refer to the abstract pathname's parent, ornull
if current file does not name a parent directory.CFile
getValue()
For internal use onlyCFile
moveTo(java.io.File dest)
Move file to new destination and return theCFile
: which points to new file.boolean
notExist()
Check whether the file or directory denoted by this abstract pathname exists.CList<java.lang.String>
readLines()
Read all lines from a file.CList<java.lang.String>
readLines(java.nio.charset.Charset charset)
Read all lines from a file.java.lang.String
readString()
Reads all characters from a file into a string, decoding from bytes to characters using default charset.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.CFile
safeRename(java.io.File destFile, boolean overwrite)
If the destination file exist and the overwrite parameter is false then do nothing.byte[]
toByteArray()
Convert file to byte array and return the result.java.lang.String
toString()
boolean
waitForWritable(int timeoutInSeconds)
Wait fortimeoutInSeconds
for file to be available and writableboolean
waitForWritable(int timeoutInSeconds, int intervalInMillSeconds)
Wait fortimeoutInSeconds
for file to be available and writableboolean
waitToExists(int timeoutInSeconds)
Wait fortimeoutInSeconds
for file to be availableboolean
waitToExists(int timeoutInSeconds, int intervalInMillSeconds)
Wait fortimeoutInSeconds
for file to be availableCFile
write(byte[] content)
Writes a byte array to a file creating the file if it does not exist.CFile
write(java.io.InputStream inputStream)
Writes a byte array to a file creating the file if it does not exist.CFile
write(java.lang.String content)
Writes a text to a file creating the file if it does not exist.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.CFile
writeJson(java.lang.Object object)
Writes an object as json to a file creating the file if it does not exist.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.-
Methods inherited from class java.io.File
canExecute, canRead, canWrite, compareTo, createTempFile, createTempFile, deleteOnExit, equals, exists, getAbsoluteFile, getAbsolutePath, getFreeSpace, getName, getParent, getPath, getTotalSpace, getUsableSpace, hashCode, isAbsolute, isDirectory, isFile, isHidden, lastModified, length, list, list, listFiles, listFiles, listFiles, listRoots, mkdir, mkdirs, renameTo, setExecutable, setExecutable, setLastModified, setReadable, setReadable, setReadOnly, setWritable, setWritable, toPath, toURI, toURL
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface org.catools.common.extensions.verify.interfaces.CBaseVerifier
_verify, _verify, _verify, _verify, _verify, _verify
-
Methods inherited from interface org.catools.common.extensions.wait.interfaces.CBaseWaiter
getDefaultWaitInSeconds, getDefaultWaitIntervalInMilliSeconds
-
Methods inherited from interface org.catools.common.extensions.states.interfaces.CFileState
equalsStringContent, isEqual, notEqualsStringContent
-
Methods inherited from interface org.catools.common.extensions.verify.interfaces.CFileVerifier
_toState, verifyEqualsStringContent, verifyEqualsStringContent, verifyEqualsStringContent, verifyEqualsStringContent, verifyEqualsStringContent, verifyEqualsStringContent, verifyIsExists, verifyIsExists, verifyIsExists, verifyIsExists, verifyIsExists, verifyIsExists, verifyIsNotExists, verifyIsNotExists, verifyIsNotExists, verifyIsNotExists, verifyIsNotExists, verifyIsNotExists, verifyNotEqualsStringContent, verifyNotEqualsStringContent, verifyNotEqualsStringContent, verifyNotEqualsStringContent, verifyNotEqualsStringContent, verifyNotEqualsStringContent
-
Methods inherited from interface org.catools.common.extensions.wait.interfaces.CFileWaiter
waitEqualsStringContent, waitEqualsStringContent, waitEqualsStringContent, waitIsExists, waitIsExists, waitIsExists, waitIsNotExists, waitIsNotExists, waitIsNotExists, waitNotEqualsStringContent, waitNotEqualsStringContent, waitNotEqualsStringContent
-
Methods inherited from interface org.catools.common.extensions.states.interfaces.CObjectState
equalsAny, equalsAny, equalsNone, equalsNone, isNotEqual, test
-
Methods inherited from interface org.catools.common.extensions.verify.interfaces.CObjectVerifier
verifyEquals, verifyEquals, verifyEquals, verifyEquals, verifyEquals, verifyEquals, verifyEqualsAny, verifyEqualsAny, verifyEqualsAny, verifyEqualsAny, verifyEqualsAny, verifyEqualsAny, verifyEqualsNone, verifyEqualsNone, verifyEqualsNone, verifyEqualsNone, verifyEqualsNone, verifyEqualsNone, verifyIsNotNull, verifyIsNotNull, verifyIsNotNull, verifyIsNotNull, verifyIsNotNull, verifyIsNotNull, verifyIsNull, verifyIsNull, verifyIsNull, verifyIsNull, verifyIsNull, verifyIsNull, verifyNotEquals, verifyNotEquals, verifyNotEquals, verifyNotEquals, verifyNotEquals, verifyNotEquals
-
Methods inherited from interface org.catools.common.extensions.wait.interfaces.CObjectWaiter
_waiter, wait, wait, wait, waitEquals, waitEquals, waitEquals, waitEqualsAny, waitEqualsAny, waitEqualsAny, waitEqualsNone, waitEqualsNone, waitEqualsNone, waitIsNotNull, waitIsNotNull, waitIsNotNull, waitIsNull, waitIsNull, waitIsNull, waitNotEquals, waitNotEquals, waitNotEquals
-
-
-
-
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)
-
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)
-
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 inCConfigs.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 filecharset
- 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 benull
- 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 classjava.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 classjava.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 tonew File(this.
.getCanonicalPath()
)- Overrides:
getCanonicalFile
in classjava.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 classjava.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, ornull
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 classjava.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 theCFile
: 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 theCList
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 theList
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 getFile.renameTo(File)
and then delete the original file- Parameters:
destFile
- where the file or directory should copy tooverwrite
- whether we should overwrite the destination if it is exists- Returns:
- CFile pointing to the destination
-
waitForWritable
public boolean waitForWritable(int timeoutInSeconds)
Wait fortimeoutInSeconds
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 fortimeoutInSeconds
for file to be available and writable- Parameters:
timeoutInSeconds
- timeout in seconds to wait for file to be availableintervalInMillSeconds
- the interval of milliseconds between each retry- Returns:
- true if file is available and writable otherwise false
-
waitToExists
public boolean waitToExists(int timeoutInSeconds)
Wait fortimeoutInSeconds
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 fortimeoutInSeconds
for file to be available- Parameters:
timeoutInSeconds
- timeout in seconds to wait for file to be availableintervalInMillSeconds
- 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 filecharset
- 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 filecharset
- 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 itsSecurityManager.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 classjava.io.File
-
getValue
public CFile getValue()
Description copied from interface:CBaseState
For internal use only- Specified by:
getValue
in interfaceCBaseState<java.io.File>
-
-