Fork me on GitHub Fork me on GitHub

File


<%
    var path = "/foo.txt";
    var file = new File(path);
    file.open("r");
    var message = "";
 
    message = file.readAll(); 
    print(message);    
    file.close();
%>

Overview

The File object is meant to provide file storage/manipulation functionality.


Operations

Operation Returns Description
File(String filename)Object
var file = new File("/templates/tmp.jag");

Takes a filename and provides a File object. (File path is relative to the Jaggery application context root)

open(r | r+ | w | w+ | a | a+)None
var file = new File("readme.txt");
file.open("r+"); 
file.close();

File can be opened for read(r), write(w), append(a), read+write(r+), write+read(w+), append+read(a+)

write(Object Object)None
var file = new File("readme.txt");
file.open("w");
file.write("Hello World!");
file.close(); 

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.

read(int numberOfCharacters)String
var file = new File("readme.txt");
file.open("r");
var text = file.read(5);
print(text);
file.close();

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.

getStream()Stream
var file = new File("readme.txt");
file.open("r");
print(file.getStream());
file.close();

Can retrieve the input stream of a file

readAll()String
var file = new File("readme.txt");
file.open("r");
print(file.readAll());
file.close();

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.

close()None
var file = new File("readme.txt");
file.open("r");
var x = file.readLine();
file.close();
file.open("a");
file.write("Hello world!");
file.close();

Users are expected to close the file after reading, writing and appending to the file.

move(String targetFileName)Boolean
var path = "/foo.txt";
var file = new File(path);
file.move("/bar.txt");

Move the file to the given target file. Returns True if the file was successfully moved.

saveAs(String targetLocation)Boolean
var path = "/foo.txt";
var file = new File(path);
file.saveAs("/newFile.txt");

Save the file to the given target file. Returns True if the file was successfully moved.

del()Boolean
var path = "/foo.txt";
var file = new File(path);
print(file.del());

Deletes this file from the file system. Returns true or false depending on the success.

getLength()Long
file.getLength()
Length of the file in bytes
getLastModified()String
file.getLastModified()
Last modified time of this file.
getName()String
file.getName()
Name of the file without the path.
isExists()Boolean
file.isExists()
Checks whether this file actually exists. Returns True if the file exists.
isDirectory()Boolean
file.isDirectory()
Checks whether the given file path is a directory
listFiles()File[]
file.listFiles()
Returns the list of files in a directory
mkdir(String name)Boolean
file.mkdir()
Creates the directory named by this abstract pathname.
getPath()String
file.getPath()
Converts this abstract pathname into a pathname String. The resulting String uses the default path-separator character to separate the names in the name sequence.