The FS file system module performs some read and write operations on system files and directories.

There are both asynchronous and synchronous versions of the methods in the module, such as the asynchronous fs.readfile () and synchronous fs.readfilesync () functions for reading file contents.

The last parameter of an asynchronous method function is a callback function. The first parameter of the callback function contains an error message.

It is recommended that you use asynchronous methods, which have higher performance, are faster, and do not block compared to synchronous methods.

Create a file

fs.writeFile(filename ,data,[options],function(err){})

If the file exists, what is written overwrites the content of the old file

Filename (String) data file name (String | Buffer) to be written content, can make the String or Buffer data. Options (Object) An option array Object containing callback {Function} callbacks that pass an exception parameter, err.Copy the code

Additional documents

fs.appendFile(path,data,[options],function(err){})

Name: file name STR: added field encode: set encoding callback: callback function that passes an exception parameter errCopy the code

Read the file

Fs. ReadFile (path, the options, the function (err, data) {})

Filename Specific path for saving files [options] Specifies options, including the encoding mode of data. Callback is a specific callback function that performs error detection and prompts.Copy the code

Whether the file exists

fs.exists(path, function(exists){})

Path Indicates the file path to be detectedCopy the code

Note that the parameters of this callback are inconsistent with those of other Node.js callbacks. It is not recommended to use fs.exists() to check whether a file exists before calling fs.open, fs.readfile (), or fs.writefile (). Doing so can cause race conditions because other processes may modify the file between calls. Instead, the user should open/read/write the file directly and deal with the error when the file does not exist.

Delete the file

fs.unlink(path,function(err){})

Path - File path callback - callback function, errCopy the code

Creating a folder

fs.mkdir(name,function(err){})

Path - File path. Callback - The callback function, err, creates the directory asynchronously. There is only one possible exception parameter to complete the callback.Copy the code

Delete folders

fs.rmdir(path,function(err){})

Path - File path. Callback - A callback function with no arguments.Copy the code

Read folder

fs.readdir(path,function(err,files){})

Path - File path. Callback - A callback function that takes two parameters: err, files. Err is an error message and files is an array list of files in the directoryCopy the code

Change a name

fs.rename(oldname,newname,function(err){})

By modifying a file name, you can change the file storage path.

There is a saying called “the method is wrong, efforts in vain” all front-end gods have their own learning methods, and learning the web front-end learning is basically the same, and for a beginner who does not know anything, will not know how to learn, which is the most direct cause of failure. So learn the web front end must have someone to guide. If you’re at a loss, you can’t find your way. You can join our front end to learn EXCHANGE Qun: 784783012.