The File class allows you to read and write text files. More...
The D3PLOT JavaScript API provides many class constants, properties and methods. For Arup to
be able to extend and enhance the API in the future any constant, property or method names beginning with a lowercase
or uppercase letter are reserved.
If you need to add your own properties or methods to one of the existing classes then to avoid any potential future conflict you
should ensure that the name begins with either an underscore (_) or a dollar sign ($) or the name is prefixed with your
own unique identifier.
For example if company 'ABC' need to add a property called 'example' then to avoid any potential future conflict use one of:
| Name | Description |
| File.DIRECTORY | Find directories |
| File.FILE | Find files |
| Name | Description |
| File.CURRENT | Seek relative to current file position |
| File.END | Seek relative to end of the file |
| File.START | Seek relative to start of the file |
| Name | Type | Description |
| filename (read only) | string | Name of the file |
| mode (read only) | constant | Mode the file was opened with (File.READ, File.WRITE etc) |
var f, line;
f = new File("/data/test/file.txt", File.READ);
while ( (line = f.ReadLine()) != undefined)
{
Message(line);
}
f.Close();
The following simple example shows how to write the numbers 1 to 10 to the file "/data/test/file.txt":
var n, line;
f = new File("/data/test/file.txt", File.WRITE);
for (n=1; n<=10; n++)
{
f.Writeln(n);
}
f.Close();
See the documentation below for more details.
Constructornew File(filename[string], mode[constant])DescriptionCreate a new File object for reading and writing text files. |
Filename of the file you want to read/write. If reading, the file must exist. If writing,
the file will be overwritten (if it exists) if mode is File.WRITE, or if mode is File.APPEND it
will be appended to if it exists, or created if it does not.
When reading a file the filename can also be a URL (uniform resource locator) in which case the file will be read from the remote site.
See File.Get() for more details on the format of the URL.
The mode to open the file with. Can be File.READ, File.WRITE or File.APPEND. For File.WRITE or File.APPEND it can also be ORed with File.BINARY if required. By default text is read and written as ASCII. To read/write text in utf-8 mode can also be ORed with File.UTF8 if required.
ReturnsFile object Return typeFile |
ExampleTo create a new file object to read file "/data/test/file.txt" var f = new File("/data/test/file.txt", File.READ);
|
Details of functionsClose()DescriptionClose a file opened by a File object. |
No arguments
ReturnsNo return value |
ExampleTo close File object f. f.Close();
|
Copy(source[string], dest[string]) [static]DescriptionCopies a file |
Source filename you want to copy.
Destination filename you want to copy source file to.
Returnstrue if copy successful, false otherwise. Return typeBoolean |
ExampleTo copy the file "/data/test/file.key" to "/data/test/file.key_backup" var copied = File.Copy("/data/test/file.key", "/data/test/file.key_backup");
|
Delete(filename[string]) [static]DescriptionDeletes a file |
Filename you want to delete.
Returnstrue if successful, false if not. Return typeBoolean |
ExampleTo delete the file "/data/test/file.key" var deleted = File.Delete("/data/test/file.key");
|
Filename you want to drive map.
The format for the file/directory name. Can be Include.NATIVE, Include.UNIX or Include.WINDOWS
Returnsstring containing drive mapped filename Return typeString |
Exists(filename[string]) [static]DescriptionCheck if a file exists. See also File.IsDirectory() and See also File.IsFile(). |
Filename you want to check for existance.
Returnstrue/false Return typeBoolean |
ExampleTo see if the file "/data/test/file.key" exists if (File.Exists("/data/test/file.key")) { do something }
|
FindFiles(directory[string], type (optional)[constant]) [static]DescriptionFind any files and/or directories in a directory. |
Directory to look for files/directories in.
Type of things to find. Can be bitwise OR of File.FILE and File.DIRECTORY. If omitted only files will be returned.
ReturnsArray of filenames/directories Return typeArray |
FindLineContaining(contain[string])DescriptionReads a line from a file which contains contain, opened for reading by a File object. Although this is possible using core JavaScript functions this function should be significantly faster as most of the processing is done by PRIMER in C rather than in the JavaScript interpreter. To enable this function to be as fast as possible a maximum line length of 512 characters is used. If you expect a file to have lines longer than 512 characters then use ReadLongLine which allows lines of any length. If one argument is used then the line must contain that string. If more than one argument is used then lines which contain any of the arguments will be returned |
String which matching lines must contain
This argument can be repeated if required
Alternatively a single array argument containing the multiple values can be given
Returnsstring read from file or undefined if end of file Return typeString |
ExampleLoop, reading lines from File object f which contain 'example'.
var line;
while ( (line = f.FindLineContaining("example") ) != undefined)
{
}
|
FindLineStarting(start[string])DescriptionReads a line from a file which starts with start, opened for reading by a File object. Although this is possible using core JavaScript functions this function should be significantly faster as most of the processing is done by PRIMER in C rather than in the JavaScript interpreter. To enable this function to be as fast as possible a maximum line length of 512 characters is used. If you expect a file to have lines longer than 512 characters then use ReadLongLine which allows lines of any length. If one argument is used then the line must start with that string. If more than one argument is used then lines which start with any of the arguments will be returned |
String which matching lines must start with
This argument can be repeated if required
Alternatively a single array argument containing the multiple values can be given
Returnsstring read from file or undefined if end of file Return typeString |
ExampleLoop, reading lines from File object f which start 'example'.
var line;
while ( (line = f.FindLineStarting("example") ) != undefined)
{
}
|
Flush()DescriptionFlushes a file opened for writing by a File object. |
No arguments
ReturnsNo return value |
ExampleTo flush File object f. f.Flush();
|
Get(url[string], filename[string], options (optional)[object]) [static]DescriptionGet a file from a remote location. See also File.Proxy(), File.ProxyPassword() and File.ProxyUsername(). |
URL (uniform resource locator) of remote file you want to get. Currently http and ftp are supported.
For http give the full address including the leading 'http://'. e.g.
'http://www.example.com/file.html'.
For ftp an optional username and password can be given. e.g.
'ftp://ftp.example.com' retrieves the directory listing for the root directory.
'ftp://ftp.example.com/readme.txt' downloads the file readme.txt from the root directory.
'ftp://user:password@ftp.example.com/readme.txt' retrieves the readme.txt file from the user's home directory.
Filename you want to save the file to.
Options for get. If 'username' and 'password' are set then basic authorization using the username and password will be used.
Object has the following properties:
| Name | Type | Description |
| password (optional) | string | Password |
| response (optional) | boolean | If set to true, then the response code will be returned instead of true/false. This can be used to retieve error messages and codes when the file is not returned successfully. |
| username (optional) | string | Username |
Returnstrue if file was successfully got, false otherwise. Return typeBoolean |
ExampleTo get the file "http://www.example.com/file.html" and save it to C:\temp: File.Get("http://www.example.com/file.html", "C:\temp\file.html");
|
IsAbsolute(filename[string]) [static]DescriptionCheck if a filename is absolute or relative. |
Filename you want to check.
Returnstrue/false Return typeBoolean |
ExampleTo see if the filename "/data/test" is absolute (which it is!) if (File.IsAbsolute("/data/test")) { do something }
|
IsDirectory(filename[string]) [static]DescriptionCheck if a filename is a directory. See also File.Exists(), File.IsFile(), File.IsReadable() and File.IsWritable(). |
Filename you want to check.
Returnstrue/false Return typeBoolean |
ExampleTo see if the filename "/data/test" is a directory if (File.IsDirectory("/data/test")) { do something }
|
IsFile(filename[string]) [static]DescriptionCheck if a filename is a file. See also File.Exists(), File.IsDirectory(), File.IsReadable() and File.IsWritable(). |
Filename you want to check.
Returnstrue/false Return typeBoolean |
ExampleTo see if the filename "/data/test" is a file if (File.IsFile("/data/test")) { do something }
|
IsReadable(filename[string]) [static]DescriptionCheck if a filename has read permissions. See also File.Exists(), File.IsDirectory() and File.IsWritable(). |
Filename you want to check.
Returnstrue/false Return typeBoolean |
ExampleTo see if the filename "/data/test" is readable if (File.IsReadable("/data/test")) { do something }
|
IsWritable(filename[string]) [static]DescriptionCheck if a filename has write permissions. If filename exists and it is a file then it is checked to see if it can be opened with write (File.APPEND permissions). If filename exists and it is a directory then the directory is checked for write permission (can files be created in the directory). If filename does not exist then it is assumed to be a file and is checked to see if it can be opened for writing (File.WRITE permissions). See also File.Exists(), File.IsDirectory() and File.IsReadable(). |
Filename you want to check.
Returnstrue/false Return typeBoolean |
ExampleTo see if the filename "/data/test" is writable if (File.IsWritable("/data/test")) { do something }
|
The name of the directory you want to create.
Returnstrue if successfully created, false if not. Return typeBoolean |
ExampleTo make the directory "/data/test" var success = File.Mkdir("/data/test");
|
Mktemp() [static]DescriptionMake a temporary filename for writing a temporary file. |
No arguments
ReturnsString name of temporary filename that can be used. Return typeString |
ExampleTo get a temp filename" var filename = File.Mktemp();
|
Proxy(name[string]) [static]DescriptionSet a proxy for files opened by http, ftp etc. See also File.Get(), File.ProxyPassword() and File.ProxyUsername(). |
The name of the proxy.
ReturnsNo return value |
ExampleTo set the proxy to "http://example.proxy.com" using port 80: File.Proxy("http://example.proxy.com:80");
|
ProxyPassword(name[string]) [static]DescriptionSet a proxy password for files opened by http, ftp etc. See also File.Get(), File.Proxy() and File.ProxyUsername(). |
Password for the proxy server.
ReturnsNo return value |
ExampleTo set the proxy password to "password": File.ProxyPassword("password");
|
ProxyUsername(username[string]) [static]DescriptionSet a proxy username for files opened by http, ftp etc. See also File.Get(), File.Proxy() and File.ProxyPassword(). |
The username for the proxy.
ReturnsNo return value |
ExampleTo set the proxy username to "username": File.ProxyUsername("username");
|
ReadAll()DescriptionReads all the remaining characters from a file opened for reading by a File object. As this function can read the entire file as a string be careful when reading large files as it will consume large amounts of memory. |
No arguments
ReturnsString. Characters read from file or undefined if end of file Return typeString |
ExampleRead all characters from File object f. var c = f.ReadAll();
|
ReadArrayBuffer(length (optional)[integer])DescriptionReads binary data from a file opened for reading by a File object.
The data is returned as an ArrayBuffer object.
For more details on how to use an ArrayBuffer see the following links: |
Number of bytes to try to read from the file. If omitted all the remaining data from the file will be read.
ReturnsArrayBuffer object or undefined if end of file Return typeArrayBuffer |
ExampleTo read data as 32bit unsigned integers from File object f. var ab = f.ReadArrayBuffer();
var u32 = new Uint32Array(ab);
for (var i=0; i<u32.length; i++
{
var value = u32[i];
}
|
Filename you want to read CSV options from.
Delimiter string to be used. Default is a comma (",").
Comment string to be used. Default is a dollar sign ("$").
Returns2d array of strings. Return typeString |
ReadChar()DescriptionReads a single character from a file opened for reading by a File object. |
No arguments
Returnscharacter read from file or undefined if end of file Return typeString |
ExampleLoop, reading characters from File object f.
var c;
while ( (c = f.ReadChar()) != undefined) { ... }
|
ReadLine()DescriptionReads a line from a file opened for reading by a File object. To enable this function to be as fast as possible a maximum line length of 512 characters is used. If you expect a file to have lines longer than 512 characters then use ReadLongLine which allows lines of any length. |
No arguments
Returnsstring read from file or undefined if end of file Return typeString |
ExampleLoop, reading lines from File object f.
var line;
while ( (line = f.ReadLine()) != undefined) { ... }
|
ReadLongLine()DescriptionReads a line from a file opened for reading by a File object. The line can be any length. If your file has lines shorter than 512 characters then you may want to use ReadLine instead which is faster. |
No arguments
Returnsstring read from file or undefined if end of file Return typeString |
ExampleLoop, reading lines from File object f.
var line;
while ( (line = f.ReadLongLine()) != undefined) { ... }
|
Rename(oldname[string], newname[string]) [static]DescriptionRename an existing file to have a different name. |
Existing filename you want to rename
New filename you want to rename to
Returnstrue if successful, false if not. Return typeBoolean |
ExampleTo rename the file "/data/test/file.key" to "/data/test/new_file.key" var size = File.Rename("/data/test/file.key", "/data/test/new_file.key");
|
Seek(offset[integer], origin (optional)[constant])DescriptionSet the current position for reading or writing in a File object. |
Offset to seek to in the file
Origin for offset. Must be one of File.START, File.END or File.CURRENT. If omitted File.START will be used.
Returnsno return value |
ExampleTo seek to the end of File f: f.Seek(0, File.END); To seek to the beginning of File f: f.Seek(0, File.START); To move forward 10 characters in File f: f.Seek(10, File.CURRENT);
|
Size(filename[string]) [static]DescriptionReturn the size of a file in bytes |
Filename you want the size of.
Returnssize in bytes Return typeNumber |
ExampleTo get the size of the file "/data/test/file.key" var size = File.Size("/data/test/file.key");
|
Tell()DescriptionReturn the current file position for a File object. Note that on Windows when reading files if the file is not opened with File.BINARY this may not return the correct file position for files with unix line endings. |
No arguments
Returnsinteger Return typeNumber |
ExampleTo get the current file position for File f: var pos = f.Tell();
|
Upload(filename[string], url[string], options (optional)[object]) [static]DescriptionUploads a file to a remote location. See also File.Proxy(), File.ProxyPassword() and File.ProxyUsername(). |
Filename you want to upload.
URL (uniform resource locator) of the remote location you want to upload the file to. Currently only http is supported.
Give the full address including the leading 'http://'. e.g.
'http://www.example.com/file.html'.
Options for upload. If both of these are set then basic authorization using the username and password will be used.
Object has the following properties:
| Name | Type | Description |
| password (optional) | string | Password |
| username (optional) | string | Username |
Returnstrue if file was successfully uploaded, false otherwise. Return typeBoolean |
ExampleTo upload the file "C:\temp\file.txt" to "http://www.example.com/file.txt": File.Upload("C:/temp/file.txt", "http://www.example.com/file.txt");
|
Write(string[Any valid javascript type])DescriptionWrite a string to a file opened for writing by a File object. Note that a carriage return is not added. |
The string/item that you want to write
ReturnsNo return value |
ExampleTo write string "Hello, world!" to File object f f.Write("Hello, world!\n");To write the title of model m to File object f f.Write("The title of model 2 is " + m.title + "\n");
|
WriteArrayBuffer(buffer[ArrayBuffer], length (optional)[integer])DescriptionWrites binary data to a file opened for writing by a File object.
The data to write is an ArrayBuffer object.
For more details on how to use an ArrayBuffer see the following links: |
ArrayBuffer to write to file
Number of bytes to write to the file. If omitted all the data in the ArrayBuffer will be written (buffer.byteLength bytes)
ReturnsNo return value |
ExampleTo write ArrayBuffer ab to File object f. f.WriteArrayBuffer(ab);
|
Writeln(string[Any valid javascript type])DescriptionWrite a string to a file opened for writing by a File object adding a carriage return. |
The string/item that you want to write
ReturnsNo return value |
ExampleTo write string "Hello, world!" to File object f automatically adding a carriage return f.Writeln("Hello, world!");To write the title of model m to File object f automatically adding a carriage return f.Writeln("The title of model 2 is " + m.title);
|