Funciones para la Manipulación de Ficheros

La clase java.io.File implementa un buen número de métodos para la manipulación de ficheros.

Análisis del Nombre de un Fichero

Existen múltiples métodos para analizar el nombre de un fichero.

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 method getAbsoluteFile with signature public 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

Estado de un Fichero

La clase java.io.File provee un buen número de métodos para consultar el estado de un fichero.

Fechas Asociadas con un Fichero

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

Longitud de un Fichero

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



Subsecciones
Casiano Rodríguez León
2010-04-30