Global variables of ballerina.file package
Variable Name | Data Type | Description |
---|---|---|
R | string | The Read access mode |
W | string | The Write access mode |
RW | string | The Read Write access mode |
A | string | The Append access mode |
RA | string | The Read Append access mode |
Functions of ballerina.file package
public function copy(File source, File destination)
Copies a file from a given location to another
Parameters:
Parameter Name | Data Type | Description |
---|---|---|
source | File | File/Directory that should be copied |
destination | File | Destination directory or path to which the source should be copied |
public function <File file> close()
Closes a given file and its stream
Receiver:
Receiver Name | Data Type | Description |
---|---|---|
file | File | The file to be closed |
public function <File file> createNewFile() (boolean, AccessDeniedError, IOError)
Creates a new file given by the path in the File struct
Receiver:
Receiver Name | Data Type | Description |
---|---|---|
file | File | The file to be created |
Return Parameters:
Return Variable | Data Type | Description |
---|---|---|
boolean | Returns true if the new file was successfully created | |
AccessDeniedError | Returns an AccessDeniedError if the user does not have the necessary permissions to create the file | |
IOError | Returns an IOError if the file could not be created due to an I/O error |
public function <File file> delete()
Deletes a file from a given location
Receiver:
Receiver Name | Data Type | Description |
---|---|---|
file | File | The file to be deleted |
public function <File file> exists() (boolean)
Checks whether the file exists
Receiver:
Receiver Name | Data Type | Description |
---|---|---|
file | File | The file to be checked for existence |
Return Parameters:
Return Variable | Data Type | Description |
---|---|---|
boolean | Returns true if the file exists |
public function <File file> getModifiedTime() (Time, AccessDeniedError, IOError)
Returns the last modified time of the file
Receiver:
Receiver Name | Data Type | Description |
---|---|---|
file | File | The file of which the modified time needs to be checked |
Return Parameters:
Return Variable | Data Type | Description |
---|---|---|
Time | Returns a Time struct | |
AccessDeniedError | Returns an AccessDeniedError if the user does not have the necessary permissions to read the file | |
IOError | Returns an IOError if the file could not be read |
public function <File file> getName() (string)
Returns the name of the file
Receiver:
Receiver Name | Data Type | Description |
---|---|---|
file | File | The file of which the name needs to be looked up |
Return Parameters:
Return Variable | Data Type | Description |
---|---|---|
string | Returns the file name as a string |
public function <File file> isDirectory() (boolean)
Checks whether the file is a directory
Receiver:
Receiver Name | Data Type | Description |
---|---|---|
file | File | The file to be checked to determine whether it is a directory |
Return Parameters:
Return Variable | Data Type | Description |
---|---|---|
boolean | Returns true if the file is a directory |
public function <File file> isReadable() (boolean)
Checks whether the user has read access to the file
Receiver:
Receiver Name | Data Type | Description |
---|---|---|
file | File | The file to be checked for read permission |
Return Parameters:
Return Variable | Data Type | Description |
---|---|---|
boolean | Returns true if the user has read access |
public function <File file> isWritable() (boolean)
Checks whether the user has write access to the file
Receiver:
Receiver Name | Data Type | Description |
---|---|---|
file | File | The file to be checked for write permission |
Return Parameters:
Return Variable | Data Type | Description |
---|---|---|
boolean | Returns true if the user has write access |
public function <File file> list() (File[], AccessDeniedError, IOError)
Lists the files in the specified directory
Receiver:
Receiver Name | Data Type | Description |
---|---|---|
file | File | The directory whose files list is needed |
Return Parameters:
Return Variable | Data Type | Description |
---|---|---|
File[] | Returns an array of File structs if successful | |
AccessDeniedError | Returns an AccessDeniedError if the user does not have the necessary permissions to read the directory | |
IOError | Returns an IOError if the directory could not be opened due to an I/O error |
public function <File file> mkdirs() (boolean, AccessDeniedError, IOError)
Creates the directory structure specified by the file struct
Receiver:
Receiver Name | Data Type | Description |
---|---|---|
file | File | The directory or directory structure to be created |
Return Parameters:
Return Variable | Data Type | Description |
---|---|---|
boolean | Returns true if the directory/directories were successfully created | |
AccessDeniedError | Returns an AccessDeniedError if the user does not have the necessary permissions to modify the directory | |
IOError | Returns an IOError if the directory could not be created |
public function <File file> open(string accessMode)
Retrieves the stream from a local file
Receiver:
Receiver Name | Data Type | Description |
---|---|---|
file | File | The file which needs to be opened |
Parameters:
Parameter Name | Data Type | Description |
---|---|---|
accessMode | string | The file access mode used when opening the file |
public function <File file> openChannel(string accessMode) (ByteChannel)
Function to return a ByteChannel related to the file. This ByteChannel can then be used to read/write from/to the file.
Receiver:
Receiver Name | Data Type | Description |
---|---|---|
file | File | The file to which a channel needs to be opened |
Parameters:
Parameter Name | Data Type | Description |
---|---|---|
accessMode | string | Specifies whether the file should be opened for reading or writing (r/w) |
Return Parameters:
Return Variable | Data Type | Description |
---|---|---|
ByteChannel | ByteChannel which will allow to perform I/O operations |
public function move(File target, File destination)
Moves a file from a given location to another
Parameters:
Parameter Name | Data Type | Description |
---|---|---|
target | File | File/Directory that should be moved |
destination | File | Location where the File/Directory should be moved to |
Structs of ballerina.file package
public struct AccessDeniedError
Represents an error which occurs when attempting to perform operations on a file without the required privileges.
Fields:
Field Name | Data Type | Description |
---|---|---|
msg | string | The error message |
cause | error | The error which caused the access denied error |
stackTrace | StackFrame[] | The stack trace of the error |
public struct File
Represents a file in the file system and can perform various file operations on this.
Fields:
Field Name | Data Type | Description |
---|---|---|
path | string | The path of the file |
public struct FileNotFoundError
Represents an error which occurs when attempting to perform operations on a non-existent file.
Fields:
Field Name | Data Type | Description |
---|---|---|
msg | string | The error message |
cause | error | The error which caused the file not found error |
stackTrace | StackFrame[] | The stack trace of the error |
public struct FileNotOpenedError
Represents an error which occurs when attempting to perform operations on a file without opening it.
Fields:
Field Name | Data Type | Description |
---|---|---|
msg | string | The error message |
cause | error | The error which caused the file not opened error |
stackTrace | StackFrame[] | The stack trace of the error |
public struct IOError
Represents an I/O error which could occur when processing a file.
Fields:
Field Name | Data Type | Description |
---|---|---|
msg | string | The error message |
cause | error | The error which caused the I/O error |
stackTrace | StackFrame[] | The stack trace of the error |