node. js Beginner, the process of synchronizing and asynchronously reading a document in a file:
1, synchronous reading:
var fs=require ("FS")
Read the document directly and assign a value to the variable by synchronizing the return value
var data=fs.readfilesync ("Input.txt");
Console.log (Data.tostring ());
2, asynchronous read:
var fs=require ("FS");
Returns the data value obtained by the callback function;
Fs.readfile ("Input.txt", function (Err,data) {
Console.log (Data.tostring ());
})
3. Asynchronous write:
var fs=require ("FS");
var data= "There's a big thing happening today!" "
Fs.writefile ("02.txt", data,function (Err) {
if (err) {
throw err;
}else{
Console.log ("File Write succeeded! ")
}
})
4. Delete files:
var fs=require ("FS");
Fs.unlink ("02.txt", function (Err) {
if (err) {
throw err;
}else{
Console.log ("Delete succeeded! ")
}
})
node. JS synchronous and asynchronous read Write delete file 1