PhantomJS - write



This method takes three parameters: Source, Content, and String/Object.

  • Source − It is the file, where the content has to be written.

  • Content − This parameter is the content that needs to be written to the file.

  • String/Object − This parameter has the mode and charset.

    • Mode − Open mode. A string made of ‘r’, ‘w’, ‘a/+’, ‘b’ characters.

    • Charset − An IANA, case insensitive, charset name.

Syntax

Its syntax is as follows −

fs.write(path, content, 'w');

Example

The following example shows how the write method works.

Command − phantomjs write.js writemode.txt

var fs = require('fs'); 
var system = require('system'); 
var path = system.args[1]; 
var md = fs.touch(path); 

console.log("file is present : "+fs.isFile(path)); 
var n = fs.write(path, 'Hello world', 'w'); 

console.log("Content present in " +path + " are :"); 
console.log(fs.read(path)); 
phantom.exit();

The above program generates the following output.

file is present : true 
Content present in writemode.txt are : 
Hello world 
phantomjs_file_system_module_methods.htm
Advertisements