File Host Object

1.0 Introduction

The File Host Object is a Mozilla Rhino Host Object designed for use in the WSO2 Mashup server environment for providing file storage/manipulation functionality.

1.1 Example

This example writes a file named hello.txt, moves it to a new directory, then reads and deletes the file.

                var text = "Hello World!";
                var file = new File("hello.txt");
                if (!file.exists)
                    file.createFile();
                file.openForWriting();
                file.write(text);
                file.close();
                var moved = file.move("backup/goodbye.txt");

                if (moved){
                    var file2 = new File("backup/goodbye.txt");
                    file2.openForReading();
                    print(file2.readAll());
                    file2.close();
                    if (!file2.deleteFile())
                        print('Unable to delete file.');
                }
            

2.0 File Object

The File object provides basic read/write access to files in the {servicename}.resources directory.

2.1 File Interface

                {
                    Constructor File(String filename);
                    function void openForReading();
                    function void openForWriting();
                    function void openForAppending();
                    function void write(Object Object);
                    function void writeLine(Object object);
                    function String read(int numberOfCharacters);
                    function String readLine();
                    function String readAll();
                    function void close();
                    function String toString();
                    function boolean createFile();
                    function boolean move(String fileName);
                    function boolean deleteFile();
                    readonly property long length;
                    readonly property String lastModified;
                    readonly property String path;
                    readonly property String name;
                    readonly property boolean exists;
                }
            

2.2 API Documentation

Member Description Supported in version
Constructor File(String filename);

The file name should be relative to the service.resources directory of the mashup service.

Throws an error if the given path represents a directory.

0.1
void openForReading(); Opens the file for reading. Throws an error if the file is already open for either appending or reading, if the file does not exist, or if the file cannot be opened for any other file system specific reason. 0.1
void openForWriting(); Opens the file for writing. The first call to write() or writeLine() on a file which has been opened for writing will overwrite the content of the file.

If the file does not exist, openForWriting() will create the file.

Throws an error if the file is already open for either appending or reading, or if the file cannot be opened for any other file system specific reason.
0.1
void openForAppending(); Opens the file for appending. Using write() or writeLine() on a file which is open for appending will write the content to the end of the file rather than the beginning.

If the file does not exist, this will create the file.

Throws an error if the file is already open for either writing or reading, if the file cannot be opened for any other file system specific reason.
0.1
void write(Object Object); Writes the String representation of the object to the file. Users can open the file for writing from the start of the file or for appending to the end of the file.

If the file is not already open for writing or appending, then calling this will automatically open the file for writing (overwriting the current content of the file if it exists).

Throws an error if the file is already open for reading.

var file = new File("readme.txt");

file.openForWriting();

//file.openForAppending();

file.write("Hello World!");

file.close();
0.1
void writeLine(Object object); Writes the String representation of the object to the file together with a line separator at the end. Users are required to open the file for writing or for appending before writing to the file.

If the file is not open for either reading or writing or appending, then calling this will automatically open the file for writing (overwriting the current content of the file).

var file = new File("readme.txt");

file.openForWriting();

//file.openForAppending();

file.writeLine("Hello World!");

file.close();
0.1
String read(int numberOfCharacters); Reads the given number of characters from the file and returns a string representation of those characters. Users are required to open the file for reading before reading from the file.

If the file is not open for either reading or writing or appending, then calling this will automatically open the file for reading.

var file = new File("readme.txt");

file.openForReading();

var text = file.read(5);

print(text);

file.close();
0.1
String readLine(); Reads a line from the file and return a string representation of the line. Users are required to open the file for reading before reading from the file.

If the file is not open for either reading or writing or appending, then calling this will automatically open the file for reading.

var file = new File("readme.txt");

file.openForReading();

var text = file.readLine();

print(text);

file.close();
0.1
String readAll(); Reads all the content in the file and returns a string representation of the content. Users are required to open the file for reading before reading from the file.

If the file is not open for either reading or writing or appending, then calling this will automatically open the file for reading.

var file = new File("readme.txt");

file.openForReading();

var text = file.readAll();

print(text);

file.close();
0.1
void close(); Users are expected to close the file after reading, writing and appending to the file.

var file = new File("readme.txt");

file.openForReading();

var x = file.readLine();

file.close();

file.openForAppending();

file.write("Hello world!");

file.close();
0.1
String toString(); Behaviour equal to the readAll();

0.1
boolean createFile(); Creates the file if it does not exist. Also creates the parent directories if they are not present.

var file = new File("readme.txt");

file.createFile();
0.1
boolean move(string targetFileName); Move the file to the given target file. Returns True if the file was successfully moved. 0.1
boolean deleteFile(); Deletes this file from the file system. Returns True if the file was successfully deleted. 0.1
readonly property long length; Length of the file in bytes 0.1
readonly property String lastModified; Last modified time of this file. 0.1
readonly property String path; Path name of the file. 0.1
readonly property String name; Name of the file without the path. 0.1
readonly property boolean exists; Checks whether this file actually exists. Returns True if the file exists. 0.1

3.0 References