zPocketScript Class Reference: File
Library: libjs_file
The file object provides file i/o operations. The file is opened on construction:
eg. myFile=new File("foo.txt",File.O_READ|File.O_WRITE)
and is closed on deletion:
eg. delete myFile;
NOTE:The File object is a seperate plugin library, not part of the core library.
Methods
- File(String filename,int mode) - construct a new File object.
eg.
eg. myFile=new File("foo.txt",File.O_READ|File.O_WRITE)
Parameters:
- filename: name of the file to open.
- mode: file open mode - a bitmask of:
- write(arg1 [,arg2,...]) - write data to the file.
Parameters:
- any - you can pass any number of parameters, of any type and they will written to the file.
Returns:
The number of bytes written.
- read(length) - read data from a file.
Parameters:
- length: an integer or floating point value for the number of bytes to read from the file.
The actual number read may be less than this if there isn't enough data available in the file.
Returns:
An Array object containing the bytes read. This can be used directly or converted to a string.
eg.
var inData=myFile.read(100); // read up to 100 bytes
var byte;
// look at each byte individually
for(var i in inData)
{
byte=inData[i];
}
// convert the byte array to a string.
var dataAsString=String.fromCharCode(inData);
- readString(length) - read data from a file.
This is a faster alternative to using String.read() and converting the data array to a string via the String.fromCharCode() method.
Parameters:
- length: an integer or floating point value for the number of bytes to read from the file.
The actual number read may be less than this if there isn't enough data available in the file.
Returns:
A String object containing the bytes read.
- tell() - get the file stream offset.
Parameters:
Returns:
Offset from the start of the file.
- seek(offset [,whence] ) - seek the file stream to a given offset.
Parameters:
- offset: offset from whence to seek to
- whence: where to seek relative to (optional, defaults to SEEK_SET)
Returns:
Properties
Constants
- O_READ The mode flag for use in creating a file object.
eg. myFile=new File("foo.txt",File.O_READ)
- O_WRITE The mode flag for use in creating a file object.
eg. myFile=new File("foo.txt",File.O_WRITE)
- SEEK_SET / SEEK_CUR / SEEK_END Whence modes for File.seek().
eg. myFile.seek(0,SEEK_END)