Introducing File System Modules:
var fs = require ("FS");
Working with ReadFile and WriteFile:
Read the contents of the file:
Exports.t1=function () {
console.log () starts reading the file. ");
Fs.readfile ("./hello.js", function (err,data) {
if (err) {return
console.log (err);
}
Console.log (data.tostring ()
); Asynchronously handles
var data=fs.readfilesync ("./hello.js");
Console.log (Data.tostring ());
Console.log ("End of File read.") ");
Synchronous processing
}
Copy file contents:
Exports.copy1=function () {
fs.readfile ("./hello.js"), function (err,data) {
if (err) {return
Console.log (err);
}
Fs.writefile ("./hello_c.js", data);}
);
To operate with Open,read,write:
exports.t2 = function () {var buf = new Buffer (1024);
Fs.open ("./hello.js", "R", function (err, FD) {(err) {return console.log (err);
Fs.read (FD, buf, 0, buf.length, 0, function (err, bytes) {if (err) {return console.log (err);
Console.log ("read" + bytes + "byte");
Console.log (buf.slice (0, bytes). toString ());
});
Fs.close (FD, function (err) {if (err) {return console.log (err);
}
});
}); }
Exports.copy2 = function () {
var buf = new Buffer (1024);
Fs.open ("./hello.js", "R", function (err, RD) {
if (err) {return
console.log (err);
}
Fs.read (Rd, buf, 0, buf.length, 0, function (err, bytes) {
if (err) {return
console.log (err);
}
Fs.open ("./hello_c.js", "w", Function (err, wd) {
(err) {return
console.log (err);
}
Fs.write (Wd,buf,0,bytes,function (err,written,buffer) {
fs.close (WD, function (ERR) {
if (err)
{ return Console.log (ERR);
}
);
Console.log (written);
if (err) {return
console.log (err);
}
}
);}; Fs.close (Rd, function (err) {
if (err) {return
console.log (err);
}
}
);}
To operate with an event loop:
exports.copy3=function () {var buf = new Buffer (1024); Var eventemitter=new event.
Eventemitter (); Eventemitter.on ("Copy.star", function () {Fs.open ('./hello.js ', ' R ', function (ERR,RD) {if (err) {return Console.log (er
R);
} eventemitter.emit ("Copy.read", RD);
}); ). On ("Copy.read", function (RD) {Fs.read (Rd,buf,0,buf.length,0,function (err,bytes) {fs.close (Rd,function (Err) {if (
ERR) {return console.log (err);
}
});
if (err) {return console.log (err);
} eventemitter.emit ("Copy.write", bytes);
}); }. On ("Copy.write", function (bytes) {fs.open ("./hello_c.js", "w", Function (ERR,WD) {if (err) {return Console.log (ERR)
; } fs.write (Wd,buf,0,bytes,function (err,writen,buffer) {fs.close (Wd,function (Err) {if (err) {return consol
E.log (ERR);
}
});
if (err) {return console.log (err);
}
});
});
}). Emit ("Copy.star"); }
Error occurs when the content is copied beyond the specified value, so you can use the Createreadstream,createwritestream method.
Exports.t3=function () {
var read=fs.createreadstream ("./hello.js");
Read.on ("Data", function (data) {
Console.log (data.tostring ());
});
Read.on ("End", function () {
console.log () file read ended. ");
})
}
Exports.copy4=function () {
var read=fs.createreadstream ("./hello.js");
var write=fs.createwritestream ("./hello_c.js");
Read.on ("Data", function (data) {
write.write (data);
});
Read.on ("End", function () {
write.end ()
});
Read.on ("Error", function (err) {
console.log (err)}
)
}
The simplest way to use a pipe:
Exports.copy5=function () {
var read=fs.createreadstream ("./hello.js");
var write=fs.createwritestream ("./hello_c.js");
Read.pipe (write);
Nodejs File System APIs