Tuesday 8 January 2013

Filesystem module (fs) in node.js

File system module is to access files on the disk.

To use the module, use   "require(fs)  "  and all the methods have synchronous and asynchronous forms.

Asyn form always takes completion call back as a last argument and 1st argument for exception. If the operation completed successfully, it returns null or undefined.

see this example

var fs=require('fs');
 fs.exists(nodemongo.txt,function(exists){ 
if(exists)
{
fs.stat(nodemongo.txt,function(error,stats){
fs.open(nodemongo.txt,function(error,ab)
{
var bf=new Buffer(stats.size);
fs.read(ab,bf,0,bf.length,null,function(error,bytesRead,buffer){
var txt=bf.toString("utf8",0,bf.length);
fs.close(ab);
    });
 });
)};
 }); 

0 comments:

Post a Comment