Show / Hide Table of Contents

Class File

Represents a file. This class is not available in CRM Online.

Use open() before you call any other methods.
Syntax
Examples
File f;

f.open("test.txt", "a+"); // Opening file appending (and creating)
f.write("test");
f.close(); // Closing the file when done writing

File g;

g.open("test.txt", "r"); // Opening file for reading only
print(g.readAll());

Constructors

File()

Initializes a new instance of the File class.

Declaration
File
Remarks

Use open() before you call any other methods.

Examples
File f;

f.open("test.txt", "a+"); // Opening file appending (and creating)
f.write("test");
f.close(); // Closing the file when done writing

File g;

g.open("test.txt", "r"); // Opening file for reading only
print(g.readAll());

Methods

close()

Closes the file. The File object is then ready to open a new file.

Declaration
Void close()
Returns
Type Description
Void
Examples
File f;

f.open("test.txt", "a+"); // Opening file appending (and creating)
f.write("test");
f.close(); // Closing the file when done writing

File g;

g.open("test.txt", "r"); // Opening file for reading only
print(g.readAll());

eof()

Returns true if a read passed the end of file has been attempted.

Declaration
Bool eof()
Returns
Type Description
Bool

True if a read passed the end of file has been attempted; otherwise, false.

Remarks

When you read lines in a loop, you can break out of the loop when this function return true.

Examples
File f;

f.open("test.txt", "a+"); // Opening file appending (and creating)
f.write("test");
f.close(); // Closing the file when done writing

File g;

g.open("test.txt", "r"); // Opening file for reading only
print(g.readAll());

flush()

Empties the output buffer, writing the data to the file.

Declaration
flush()
Returns
Type Description
Void
Examples
File f;

f.open("test.txt", "a+"); // Opening file appending (and creating)
f.write("test");
f.close(); // Closing the file when done writing

File g;

g.open("test.txt", "r"); // Opening file for reading only
print(g.readAll());

open(String,String)

Opens the file to be available for other calls.

Declaration
Bool open(String filename, String mode)
Parameters
Type Name Description
String filename

Path to the file

String mode

How to open the file.

Returns
Type Description
Bool
Remarks

Use open(String,String,String) to provide a codepage.

Modes:
ModeDescription
r or rbOpen file for reading.
w or wbTruncate to zero length or create a file for writing.
a or abAppend; open or create a file for writing at the end-of-file.
r+ or rb+ or r+bOpen file for update (reading and writing).
w+ or wb+ or w+bTruncate to zero length or create a file for update.
Examples
File f;

f.open("test.txt", "a+"); // Opening file appending (and creating)
f.write("test");
f.close(); // Closing the file when done writing

File g;

g.open("test.txt", "r"); // Opening file for reading only
print(g.readAll());

open(String,String,String)

Opens the file with a codepage to be available for other calls.

Declaration
Bool open(String filename, String mode, String codepage)
Parameters
Type Name Description
String filename

Path to the file

String mode

How to open the file

String codepage

What text codepage the file is in. Default is ISO-8859-1 (Latin-1), but UTF-8 is common for Unicode text files.

Returns
Type Description
Bool
Remarks

Use open(String,String) if you don't need to specify codepage.

Modes:
ModeDescription
r or rbOpen file for reading.
w or wbTruncate to zero length or create a file for writing.
a or abAppend; open or create a file for writing at the end-of-file.
r+ or rb+ or r+bOpen file for update (reading and writing).
w+ or wb+ or w+bTruncate to zero length or create a file for update.
Examples
File f;

f.open("test.txt", "a+"); // Opening file appending (and creating)
f.write("test");
f.close(); // Closing the file when done writing

File g;

g.open("test.txt", "r"); // Opening file for reading only
print(g.readAll());

readAll()

Returns all text from the file.

Declaration
String readAll()
Returns
Type Description
String
Examples
File f;

f.open("test.txt", "a+"); // Opening file appending (and creating)
f.write("test");
f.close(); // Closing the file when done writing

File g;

g.open("test.txt", "r"); // Opening file for reading only
print(g.readAll());

readBinary()

Reads the file and returns binary data in a Byte array.

Declaration
Byte readBinary()
Returns
Type Description
Byte[]

Binary data.

Examples
File f;

f.open("test.txt", "a+"); // Opening file appending (and creating)
f.write("test");
f.close(); // Closing the file when done writing

File g;

g.open("test.txt", "r"); // Opening file for reading only
print(g.readAll());

readLine()

Returns the next line from the file or a null string if no more data in the file.

Declaration
String readLine()
Returns
Type Description
String

The next line from the file or a null string if no more data in the file.

Examples
File f;

f.open("test.txt", "a+"); // Opening file appending (and creating)
f.write("test");
f.close(); // Closing the file when done writing

File g;

g.open("test.txt", "r"); // Opening file for reading only
print(g.readAll());

rewind()

Sets the current file pointer to the beginning of the file.

Declaration
Void rewind()
Returns
Type Description
Void
Examples
File f;

f.open("test.txt", "a+"); // Opening file appending (and creating)
f.write("test");
f.close(); // Closing the file when done writing

File g;

g.open("test.txt", "r"); // Opening file for reading only
print(g.readAll());

unlink()

Deletes the file.

Declaration
Bool unlink()
Returns
Type Description
Bool
Examples
File f;

f.open("test.txt", "a+"); // Opening file appending (and creating)
f.write("test");
f.close(); // Closing the file when done writing

File g;

g.open("test.txt", "r"); // Opening file for reading only
print(g.readAll());

write(String)

Writes data to the file.

Declaration
Void write(String data)
Parameters
Type Name Description
String data

String to write to the file.

Returns
Type Description
Void
Remarks

Can be called many times, adding more and more data to the file.

Examples
File f;

f.open("test.txt", "a+"); // Opening file appending (and creating)
f.write("test");
f.close(); // Closing the file when done writing

File g;

g.open("test.txt", "r"); // Opening file for reading only
print(g.readAll());

writeBinary(Byte[])

Writes binary data to file.

Declaration
Void writeBinary(Byte[] data)
Parameters
Type Name Description
Byte[] data

Data in a Byte array that you want to write to the file.

Returns
Type Description
Void
Examples
File f;

f.open("test.txt", "a+"); // Opening file appending (and creating)
f.write("test");
f.close(); // Closing the file when done writing

File g;

g.open("test.txt", "r"); // Opening file for reading only
print(g.readAll());
In This Article
© SuperOffice. All rights reserved.
SuperOffice |  Community |  Release Notes |  Privacy |  Site feedback |  Search Docs |  About Docs |  Contribute |  Back to top