In this section, we look at the capabilities provided for IO to objects on the filesystem, such as reading writing and appending files.
File modes
First a brief word about file modes. In Jaxer when you open a file you provide a file mode string to the open method to indicate how you want to access the file. The options for this are show below. Use the binary mode strings when the data stored is not regular text. This is sometimes referred to as 'binary safe' file access.
Reading from files
First we need a file to read from. Let's use the readme file shipped with Jaxer (jaxer/README-JAXER.TXT)
This gives us a file object named 'readme' which references the README-JAXER.TXT file in the folder containing the jaxer executable. By creating the file reference in this way, we get the file referenced in a cross platform manner.
Reading the entire file
Read the entire contents of that file into a string:
Note: Check that the file exists and is readable before you open it. To open the file for reading, use the access mode to the open method, in this case 'r'. By default files are opened for read, so in this case, you could simple have omitted the access mode.
The above code will output the contents of the file into the log.
6:38:13 03/05/2008 [ 4512] [INFO] [JS Framework] [framework.] This folder contains Jaxer, the Aptana Ajax Server.
To get started with Jaxer, it's easiest to start with Aptana Studio,
which embeds Jaxer and offers samples, content assist, specialized views,
and so on. You can also use the standalone Jaxer Package, which
includes this folder as well as a pre-configured Apache server and
scripts to start and stop everything.
To update your Jaxer, update your Aptana Studio, or for the standalone
package visit http://www.aptana.com/jaxer/download and see the instructions.
For more information, please visit http://www.aptana.com/jaxer/
and join the conversation.
- The Aptana team
Reading the file as an array of lines
Sometimes it is useful to get the contents of a file as an array of individual lines. The readAllLines() method provides that capability. It is worth noting that this will use the platform specific line terminators to split the contents into individual lines, and as such is safer than using the String split('\n') method.
This would result in a log entry with the number of lines found in the original file.
16:52:27 03/05/2008 [ 4512] [INFO] [JS Framework] [framework.] 17
Reading the file one char at a time
The read() method takes a parameter representing the number of bytes to read from the file. So, to read the file one byte at a time, we can pass the value to the read() method.
The above code would write an entry to the log for each byte read from the file.
Writing to files
Creating an Empty file
To create an empty file we use the create() method.
The above code would create an empty file named 'myFile.txt' in the same folder as the current page.
Appending content to files
Appending to a file is done by opening a file in 'append mode' and writing data to that file.
Note the 'a' parameter provided to the open() method to indicate the file should be opened in append mode. Before opening the file it is usually a good idea to check that it exists and is writable.
Replacing the content of files
Replace the contents of a file (or overwriting) is done by opening a file in 'write mode' and writing data to that file.
Note the 'w' parameter provided to the open() method to indicate the file should be opened in write mode. Again we check before opening the file that it exists and is writable.