File Object

ICE Data Services -

File Object

 

Accessing Files with EFS. (File.efs)

 

All Files created, read , or written using the File class will be read from the FormulaOutputRoot. You can configure this in the Tools->Settings menu.

 

File Object

File(filename)

 

var f = new File("TestFolder/testReadWrite.txt")

 

This constructs a new File. At this point, after construction, the file is not open for reading or writing. You can use functions such as f.exists() to determine if the file exists, or f.getLength() to get the length of the file.

 

You can use the File object to write to or read from a file on the local machine. For security reasons, you cannot programmatically access the file system of client machines and y ou can only access or create files/directories within the FormulaOutputRoot

 

 

Methods

 

void close()

 

Closes an open file. When your application is finished with a file, you should close the file by calling the close method.

 

Boolean eof()

 

Use the eof method to determine whether the position of the pointer is beyond the end of a file.

 

A call to setPosition resulting in a location greater than fileObjectName.getLength places the pointer beyond the end of the file. Because all read operations also move the pointer, a read operation that reads the last byte of data (or character) in a file positions the pointer beyond the end of the file.

The eof method returns true if the pointer is beyond the end of the file; otherwise, it returns false.

 

Example:

 

 var f = new File("TestFolder/testReadWrite.txt");     

 f.open("rt");           

 

 var line;            

 while(!f.eof()) {                  

  line = f.readln();                  

  debugPrintln("Line: [" + line + "]");      

 }            

 

 f.close();

 

 

Boolean exists()

Tests whether a file exists.

 

Example:

 

 var f = new File("TestFolder/testReadWrite.txt");

 if(f.exists()) {          

 

  f.open("rt");

  var line;           

 

  while(!f.eof()) {                      

   line = f.readln();                      

   debugPrintln("Line: [" + line + "]");          

  }                

 

  f.close();  

 }

 

 

 

void flush()

 

Writes the content of the internal buffer to a file. When you write to a file with any of the File object methods (write or writeln), the data is buffered internally. The flush method writes the buffer to the physical file.

 

Integer getLength()

 

Returns the length of a file.

 

Integer getPosition()

 

Returns the current position of the pointer in an open file. Use the getPosition method to determine the position of the pointer in a file. The getPosition method returns the current pointer position; the first byte in a file is byte 0.

 

Boolean isOpen()

 

Returns true if the file is open.

 

Boolean mkDir()

 

Creates a directory as specified in the file constructor. Returns true if successful

 

Example:

 

 var f = new File("TestFolder");  

 f.mkDir();

 

 

Boolean open(mode)

 

Use the open method to open a file before you read from it or write to it. If the file is already open, the method fails and has no effect. The open method returns true if it is successful; otherwise, it returns false.

 

The mode parameter is a string that specifies whether to open the file to read, write, or append data.

 

The possible values for mode are as follows:

 

  • rt opens a file for reading. If the file exists, the method succeeds and returns true; otherwise, the method fails and returns false.
  • wt opens a file for writing. If the file does not already exist, it is created; otherwise, it is overwritten. This method always succeeds and returns true.
  • at opens a file for appending (writing at the end of the file). If the file does not already exist, it is created. This method always succeeds and returns true.
  • rt+ opens a file for reading and writing. If the file exists, the method succeeds and returns true; otherwise, the method fails and returns false. Reading and writing commence at the beginning of the file. When writing, characters at the beginning of the file are overwritten.
  • wt+ opens a file for reading and writing. If the file does not already exist, it is created; otherwise, it is overwritten. This method always succeeds and returns true.
  • at+ opens a file for reading and appending. If the file does not already exist, it is created. This method always succeeds and returns true. Reading and appending commence at the end of the file.

 

When your application is finished with a file, you should close the file by calling the close method.

 

String read(numbytes)

 

Reads data from a file into a string. The read method reads the specified number of characters from a file, starting from the current position of the pointer. If you attempt to read more characters than the file contains, the method reads as many characters as possible. This method moves the pointer the number of characters specified by the count parameter.

 

The read method returns the characters it reads as a string.

 

String readln()

 

Reads the current line from an open file and returns it as a string. The readln method reads the current line of characters from a file, starting from the current position of the pointer. If you attempt to read more characters than the file contains, the method reads as many characters as possible. This method moves the pointer to the beginning of the next line. See the File object for a description of the pointer.

 

The readln method returns the characters it reads as a string.

 

The line separator characters ("\r" and "\n" on Windows platforms) are not included in the string that the readln method returns. The \r character is skipped; \n determines the actual end of the line.

 

Use the readln method to read information from a text file; You can use the writeln method to write data read by the readln method to a file.

 

setPosition(position)

 

Positions a pointer in an open file. The position argument is a positive integer that moves the pointer the specified number of bytes relative to the beginning of file. Position 0 represents the beginning of a file. The end of a file is indicated by fileObject.getLength().

 

Boolean write(string)

 

Writes data from a string to a file. The write method writes the string specified as string to the file specified as fileObject. Use the write method to write data to a text file. You can use the read method to read data from a file to a string for use with the write method.

 

Boolean writeln(string)

 

Writes a string and a carriage return to a file. The writeln method writes the string specified as string to the file specified as fileObject. Each string is followed by the carriage return/line feed character ("\r\n" on Windows platforms). Use the writeln method to write data to a text file; use the writeByte method to write data to a binary file. You can use the readln method to read data from a file to a string for use with the writeln method.