La clase java.io.File implementa un buen número de métodos para la manipulación de ficheros.
The method getPath public String getPath()
converts this
abstract pathname into a pathname string. The resulting string uses
the default name-separator character to separate the names in the
name sequence.
groovy:000> new File('io/cliargs.groovy').getPath() ===> io/cliargs.groovy
The methodgetAbsoluteFile
with signaturepublic File getAbsoluteFile()
returns the absolute form of this abstract pathname.
He aquí algunos ejemplos de uso de algunas funcionesr relacionadas:
groovy:000> new File('io/cliargs.groovy').getAbsoluteFile() ===> /Users/casianorodriguezleon/src/groovy/io/cliargs.groovy groovy:000> new File('io/cliargs.groovy').getCanonicalPath() ===> /Users/casianorodriguezleon/src/groovy/files/cliargs.groovy groovy:000> new File('io/cliargs.groovy').getCanonicalFile() ===> /Users/casianorodriguezleon/src/groovy/files/cliargs.groovy groovy:000> new File('io/cliargs.groovy').toURL() ===> file:/Users/casianorodriguezleon/src/groovy/io/cliargs.groovy groovy:000> new File('io/cliargs.groovy').getParent() ===> io groovy:000> new File('io/cliargs.groovy').getParentFile() ===> io groovy:000> new File('io/cliargs.groovy').getName() ===> cliargs.groovy
La clase java.io.File provee un buen número de métodos para consultar el estado de un fichero.
public boolean canRead()
Tests whether the application can read the file denoted by this abstract pathname.
Returns: true if and only if the file specified by this abstract pathname exists and can be read by the application; false otherwise
groovy:000> new File('io/cliargs.groovy').canRead() ===> true
public boolean canWrite()
Tests whether the application can modify to the file denoted by this abstract pathname.
Returns: true if and only if the file system actually contains a file denoted by this abstract pathname and the application is allowed to write to the file; false otherwise.
groovy:000> new File('io/cliargs.groovy').canWrite() ===> true
public boolean exists()
Tests 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 exists; false otherwise
public boolean isDirectory()
Tests whether the file denoted by this abstract pathname is a directory.
Returns: true if and only if the file denoted by this abstract pathname exists and is a directory; false otherwise
groovy:000> new File('io/cliargs.groovy').isDirectory() ===> false groovy:000> new File('io').isDirectory() ===> true
public boolean isFile()
Tests whether the file denoted by this abstract pathname is a normal file. A file is normal if it is not a directory and, in addition, satisfies other system-dependent criteria. Any non-directory file created by a Java application is guaranteed to be a normal file.
Returns: true if and only if the file denoted by this abstract pathname exists and is a normal file; false otherwise
public boolean isHidden()
Tests whether the file named by this abstract pathname is a hidden file. The exact definition of hidden is system-dependent. On UNIX systems, a file is considered to be hidden if its name begins with a period character ('.'). On Microsoft Windows systems, a file is considered to be hidden if it has been marked as such in the filesystem.
Returns: true if and only if the file denoted by this abstract pathname is hidden according to the conventions of the underlying platform Throws:
groovy:000> new File('io/cliargs.groovy').isHidden() ===> false groovy:000> new File('.svn').isHidden() ===> true
public boolean isFile()
Tests whether the file denoted by this abstract pathname is a normal file. A file is normal if it is not a directory and, in addition, satisfies other system-dependent criteria. Any non-directory file created by a Java application is guaranteed to be a normal file.
Returns: true if and only if the file denoted by this abstract pathname exists and is a normal file; false otherwise
groovy:000> new File('io/cliargs.groovy').isFile() ===> true
The method
public long lastModified()
returns the time that the file denoted by this abstract pathname was last modified.
Returns: A long value representing the time the file was last modified, measured in milliseconds since the epoch (00:00:00 GMT, January 1, 1970), or 0L if the file does not exist or if an I/O error occurs
groovy:000> a = new File('io/cliargs.groovy').lastModified() ===> 1267353623000 groovy:000> new Date(a) ===> Sun Feb 28 10:40:23 GMT 2010
The method public long length()
returns the length of the file denoted by this abstract pathname. The return value is unspecified if this pathname denotes a directory.
Returns: The length, in bytes, of the file denoted by this abstract pathname, or 0L if the file does not exist
groovy:000> new File('io/cliargs.groovy').length() ===> 3318 groovy:000> 'ls -l io/cliargs.groovy'.execute().text ===> -rw-r--r-- 1 casianorodriguezleon staff 3318 28 feb 10:40 io/cliargs.groovy