The File class allows you to read and write from text files. More...
The REPORTER 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.APPEND | Flag to open file for appending |
| File.EOF | Flag to indicate end of file |
| File.READ | Flag to open file for reading |
| File.WRITE | Flag to open file for writing |
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 already exists
The mode to open the file with. Can be File.READ, File.WRITE or File.APPEND
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();
|
ConvertSeparators(filename[string]) [static]DescriptionConvert directory separators to the correct type for this operating system |
Filename you want to convert separators on.
Returnsstring filename Return typeString |
Examplee.g. on windows the filename "c:/test/file.key" would be converted to "c:\test\file.key" by var converted = File.ConvertSeparators("c:/test/file.key");
|
Copy(source[string], dest[string]) [static]DescriptionCopy a file |
Source filename you want to copy.
Destination filename you want to copy source file to. Note that if a file with the name dest already exists it will not be overwritten. Delete the file first with File.Delete().
Returnstrue if copy successful, false otherwise. Return typeBoolean |
ExampleTo copy the file "/data/test/file.txt" to "/data/test/file.txt_backup" var copied = File.Copy("/data/test/file.txt", "/data/test/file.txt_backup");
|
Delete(filename[string]) [static]DescriptionDelete a file |
Filename you want to delete.
Returnstrue if successful, false if not Return typeBoolean |
ExampleTo delete the file "/data/test/file.txt" var deleted = File.Delete("/data/test/file.txt");
|
Directory(filename[string]) [static]DescriptionExtract directory name from an absolute filename |
Absolute filename you want to extract directory from.
Returnsstring directory Return typeString |
ExampleTo extract the directory "/data/test/" from file "/data/test/file.key" var directory = File.Directory("/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 |
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], pattern[string], recursive[boolean]) [static]DescriptionFind any files in a directory (and subdirectories if required) matching a pattern |
Directory to look for files in
Pattern to use to find matching files
If Reporter should look for files recursively or not
Returnsarray filenames Return typeString |
ExampleTo find all of the files matching the pattern "*.key" recursively from directory /data/test var filelist = File.FindFiles("/data/test/", "*.key", true);
|
FindLineContaining(contain[string])DescriptionReads a line from a file which contains contain, opened for reading by a File object. To enable this function to be as fast as possible a maximum line length of 256 characters is used. If you expect a file to have lines longer than 256 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 (maximum length of 256 characters).
This argument can be repeated if required
Alternatively a single array argument containing the multiple values can be given
Returnsstring read from file or File.EOF if end of file Return typeString |
ExampleLoop, reading lines from File object f which contain 'example'.
var line;
while ( (line = file.FindLineContaining("example") ) != File.EOF)
{
}
|
FindLineMatching(regex[RegExp])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 256 characters is used. If you expect a file to have lines longer than 256 characters then use ReadLongLine which allows lines of any length. Note that this may be much slower than FindLineStarting or FindLineContaining, especially if the regular expression is very complicated. |
Regular expression which matching lines must match with.
Returnsstring read from file or File.EOF if end of file Return typeString |
ExampleLoop, reading lines from File object f which contain digits.
var line;
var regex = new RegExp("\\d+");
while ( (line = file.FindLineMatching(regex) ) != File.EOF)
{
}
|
FindLineStarting(start[string])DescriptionReads a line from a file which starts with start, opened for reading by a File object. To enable this function to be as fast as possible a maximum line length of 256 characters is used. If you expect a file to have lines longer than 256 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 (maximum length of 256 characters).
This argument can be repeated if required
Alternatively a single array argument containing the multiple values can be given
Returnsstring read from file or File.EOF if end of file Return typeString |
ExampleLoop, reading lines from File object f which start 'example'.
var line;
while ( (line = file.FindLineStarting("example") ) != File.EOF)
{
}
|
Flush()DescriptionFlushes a file opened for writing by a File object. |
No arguments
ReturnsNo return value |
ExampleTo flush File object f. f.Flush();
|
IsAbsolute(filename[string]) [static]DescriptionCheck if a filename is absolute |
Filename you want to test if absolute.
Returnstrue/false Return typeBoolean |
ExampleTo see if the file "/data/test/file.key" is absolute if (File.IsAbsolute("/data/test/file.key")) { do something }
|
IsDirectory(filename[string]) [static]DescriptionCheck if a filename is a directory |
Filename you want to test to see if it is a directory.
Returnstrue/false Return typeBoolean |
ExampleTo see if "/data/test" is a directory if (File.IsDirectory("/data/test")) { do something }
|
IsFile(filename[string]) [static]DescriptionCheck if a filename is a file |
Filename you want to test to see if it is a file (i.e. not a directory).
Returnstrue/false Return typeBoolean |
ExampleTo see if "/data/test" is a file if (File.IsFile("/data/test")) { do something }
|
Mkdir(name[string]) [static]Descriptionmakes a directory |
Directory you want to create.
Returnstrue if successful Return typeBoolean |
ExampleTo make directory "/data/test" if it does not exist: if (!File.IsDirectory("/data/test")) File.Mkdir("/data/test");
|
Move(source[string], dest[string]) [static]DescriptionMove a file |
Source filename you want to move.
Destination filename you want to move (rename) source file to. Note that if a file with the name dest already exists it will not be overwritten. Delete the file first with File.Delete().
Returnstrue if move successful, false otherwise. Return typeBoolean |
ExampleTo move the file "/data/test/file.txt" to "/data/test/file.txt_backup" var moved = File.Move("/data/test/file.txt", "/data/test/file.txt_backup");
|
ReadChar()DescriptionReads a single character from a file opened for reading by a File object. |
No arguments
Returnscharacter read from file or File.EOF 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 256 characters is used. If you expect a file to have lines longer than 256 characters then use ReadLongLine which allows lines of any length. |
No arguments
Returnsstring read from file or File.EOF if end of file Return typeString |
ExampleLoop, reading lines from File object f.
var line;
while ( (line = file.ReadLine() ) != File.EOF)
{
}
|
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 256 characters then you may want to use ReadLine instead which is faster. |
No arguments
Returnsstring read from file or File.EOF if end of file Return typeString |
ExampleLoop, reading lines from File object f.
var line;
while ( (line = file.ReadLongLine() ) != File.EOF)
{
}
|
Seek(position[integer])DescriptionSets the file position for reading a file |
Position you want to seek to.
ReturnsNo return value |
ExampleTo seek to position 1000 in file object f: f.Seek(1000);
|
SimplifyName(filename[string]) [static]DescriptionSimplify the name of a file by removing //, /./ and /../ |
Filename you want to simplify.
Returnsstring filename Return typeString |
ExampleTo simplify the filename "/data/test//../file.key" var simple = File.SimplifyName("/data/test//../file.key");This simplifies to "/data/file.key" |
Size(filename[string]) [static]DescriptionGet the size of a file |
File you want to find the size of.
Returnsinteger Return typeNumber |
ExampleTo find the size of file "/data/test" var size = File.Size("/data/test");
|
Write(string[Any valid javascript type])DescriptionWrite a string to a file opened for writing by a File object |
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 2 to File object f f.Write("The title of model 2 is " + models[2].title + "\n");
|