Chapter 7. Working with Files

Reading and writing to files are some of the basic tasks in programming. These functions are commonly used in reading and saving the configuration of the application, persisting important data, or for logging application activities. PhantomJS provides a rich FileSystem API that supports most of the file handling and operations needed. The API contains similar functions that can be found in other programming language. In this chapter we will cover the checking, loading, and writing of files using PhantomJS's built-in file handling capabilities.

PhantomJS FileSystem API

The FileSystem module of PhantomJS provides mechanisms to manipulate files, folders, paths, and contents. It is also capable of handling file object streams. The API is handy and very easy to use.

Let us start with a very simple example, using the API to display the folder we are working on.

var fs = require('fs'),
console.log("Working Directory: " + fs.workingDirectory);
phantom.exit(0);

In the first line, we reference an instance of the FileSystem module using the require keyword and passing fs as the module name. With this, we can now access all the file and folder functions.

PhantomJS FileSystem API
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.80.123