This paper mainly introduces the method of node. js to read and write the system files and directories based on FS module, and analyzes the Nodejs using FS module to read, write, create and delete the files and directories according to the instance form, and the friends can refer to them.
If you want to use this module, first need to introduce, FS already belongs to node. js comes with the module, so directly introduced can
var fs = require (' FS ');
1. Read the file ReadFile method using
fs.readFile(filename,[option],callback)
method to read the file.
Parameter description:
FileName String filename
Option Object
Encoding String |null default=null
Flag String default= ' R '
Callback Function
Set encoding format Fs.readfile ('./test.txt ', ' utf-8 ', function (err, data) {// Read file failed/error if (err) { throw err; } //Read file Success Console.log (' utf-8: ', data.tostring ());});
2. Write the file WriteFile method using
Use fs.writeFile(filename,data,[options],callback)
write content to file.
Parameter description:
FileName String filename
Data String|buffer
Option Object
Encoding String |nulldefault= ' Utf-8 '
Mode number default=438 (aka 0666 in octal)
Flag Stringdefault= ' W '
Callback Function
var fs = require (' FS '); Introduce the FS module//write the file contents (if the file does not exist will create a file)//write will first empty the file Fs.writefile ('./test2.txt ', ' test test ', function (err) { if (err) { C5/>throw err; } Console.log (' Saved. '); After the write succeeds read the test fs.readfile ('./test2.txt ', ' utf-8 ', function (err, data) { if (err) { throw err; } Console.log (data);}) ;
Because the default flag= ' W ' is write, the file will be emptied, and if you want to append, you can pass a flag parameter as follows.
Flag value, R for read file, W for write file, and a for append.
var fs = require (' FS '); Introduce FS module//write file contents (if file does not exist will create a file)//pass append parameter {' flag ': ' A '}fs.writefile ('./test2.txt ', ' test test ', {' flag ': ' A '}, func tion (err) { if (err) { throw err; } Console.log (' Saved. '); After the write succeeds read the test fs.readfile ('./test2.txt ', ' utf-8 ', function (err, data) { if (err) { throw err; } Console.log (data);}) ;
3. Create directory mkdir use
Using the fs.mkdir(path,[mode],callback)
Create directory, path is the directory that needs to be created, [mode] is the permission for the directory (default is 0777), and callback is the callback function.
var fs = require (' FS '); Introduce FS module//Create newdir directory Fs.mkdir ('./newdir ', function (err) { if (err) { throw err; } Console.log (' Make dir success. ');});
4. Read the directory Readdir
var fs = require (' FS '); Introducing the FS module Fs.readdir ('./newdir ', function (err, files) { if (err) { throw err; } Files is an array //Each element is the name of a file or folder under this directory console.log (files);});
5. deleting files unlink
var fs = require (' FS '); Introducing the FS module Fs.unlink ('./newfile.txt ', function (err) { if (err) { console.log (err); return false; } Console.log ("Success");});
6. Delete the empty folder RmDir
var fs = require (' FS '); Introducing the FS module Fs.rmdir ('./newdir ', function (err) { if (err) { console.log (err); return false; } Console.log ("Success");});
The above content is node. JS is based on the FS module on the system files and directories to read and write operations in detail, I hope to help everyone.
How to implement a simple node. js Scaffold
PHP and node. js
Node. js Operation MongoDB Code case sharing