Module jls.io.File
Provides file system abstraction.
A File extends Path by adding file system manipulation, such as deleting, renaming, listing.
Usage:
local dir = File:new('work') for _, file in ipairs(dir:listFiles()) do if file:isFile() then print('The file "'..file:getPath()..'" length is '..tostring(file:length())) end end
Class File
File:new ([parent], path) | Creates a new File with the specified name. |
file:getPath () | Returns the path of the file as a string. |
file:getParentFile () | Returns the parent of this file entry as a File. |
file:exists () | Indicates whether or not this file exists. |
file:isFile () | Indicates whether or not this file entry is a file. |
file:isDirectory () | Indicates whether or not this file entry is a directory. |
file:length () | Returns the length of the file entry represented by this file. |
file:lastModified () | Returns last modified time of the file entry represented by this file. |
file:setLastModified (time) | Sets the last modified time of the file entry represented by this file. |
file:mkdir () | Creates the directory named by this file entry. |
file:renameTo (dest) | Renames this file entry. |
file:delete () | Deletes this file entry. |
file:list () | Returns an array of strings naming the file system entries in the directory represented by this file. |
file:listFiles ([filter]) | Returns an array of files in the directory represented by this file. |
file:readAll (maxSize) | Returns the content of this file. |
file:write (data, append) | Writes the specified data into this file. |
Class File
A File class.
A File instance represents a file or a directory.
- File:new ([parent], path)
-
Creates a new File with the specified name.
See Path
Parameters:
Returns:
-
a new File
Usage:
local workingDirectory = File:new('work') local configurationPath = File:new(workingDirectory, 'configuration.json')
- file:getPath ()
-
Returns the path of the file as a string.
The result is normalized with the OS separator.
Returns:
-
string
the path of the file.
Usage:
local configurationPath = File:new('work/configuration.json') configurationPath:getPath() -- returns 'work/configuration.json'
- file:getParentFile ()
-
Returns the parent of this file entry as a File.
Returns:
-
the parent of this file as a File.
Usage:
local configurationPath = File:new('work/configuration.json') configurationPath:getParentFile():getName() -- returns 'work'
- file:exists ()
-
Indicates whether or not this file exists.
Returns:
-
boolean
true when this file exists, false otherwise.
- file:isFile ()
-
Indicates whether or not this file entry is a file.
Returns:
-
boolean
true when this file entry exists and is a file, false otherwise.
- file:isDirectory ()
-
Indicates whether or not this file entry is a directory.
Returns:
-
boolean
true when this file entry exists and is a directory, false otherwise.
- file:length ()
-
Returns the length of the file entry represented by this file.
Returns:
-
boolean
the size of this file entry or 0.
- file:lastModified ()
-
Returns last modified time of the file entry represented by this file.
The time is given as the number of milliseconds since the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
Returns:
-
number
the last modified time of this file entry or 0.
- file:setLastModified (time)
-
Sets the last modified time of the file entry represented by this file.
Parameters:
- time number the last modified time or nil to set the current time.
- file:mkdir ()
-
Creates the directory named by this file entry.
Returns:
-
boolean
true if the directory is created.
- file:renameTo (dest)
-
Renames this file entry.
Parameters:
- dest the new name of this file as a File or a string.
Returns:
-
boolean
true if the file is renamed.
In case of error, it returns nil, plus a string describing the error and the error code.
- file:delete ()
-
Deletes this file entry.
This file may points to a file or an empty directory.
Returns:
-
boolean
true if the file entry is deleted.
- file:list ()
-
Returns an array of strings naming the file system entries in the directory represented by this file.
Returns:
-
table
An array of strings naming the file system entries or nil.
- file:listFiles ([filter])
-
Returns an array of files in the directory represented by this file.
Parameters:
- filter function a filter function that will be called on each file. (optional)
Returns:
-
table
An array of files or nil.
- file:readAll (maxSize)
-
Returns the content of this file.
Parameters:
- maxSize number the maximum file size to read
Returns:
-
string
the content of this file or nil.
- file:write (data, append)
-
Writes the specified data into this file.
Parameters:
- data the data to write as a string or an array of string
- append boolean true to indicate that the data should be appended to this file