PhantomJS - write 方法

此方法采用三个参数:Source、ContentString/Object

  • Source − 是需要写入内容的文件。

  • Content − 此参数是需要写入文件的内容。

  • String/Object − 此参数具有模式和字符集。

    • Mode − 打开模式。由'r'、'w'、'a/+'、'b'字符组成的字符串。

    • Charset − IANA 不区分大小写的字符集名称。

语法

其语法如下 −

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

示例

以下示例显示了 write 方法的工作原理。

命令 − 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();

上述程序生成以下输出

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

phantomjs_file_system_module_methods.html