Previous remarks
The fs file system is used to read and write system files and directories. This paper will introduce the js file system in detail.
Summary
File I/O is provided by standard POSIX functions with simple encapsulation. Use this module through require('fs'). All methods have asynchronous and synchronous forms.
The asynchronous form always takes the completion callback as its last parameter. The parameters passed to complete the callback depend on the specific method, but the first parameter is always left to the exception. If the operation completes successfully, the first parameter will be null or undefined
//Asynchronous examples var fs = require('fs'); fs.unlink('/tmp/hello', function(err){ if (err) throw err; console.log('successfully deleted /tmp/hello'); });
When synchronization is used, any exception is thrown immediately. You can use try/catch to handle exceptions or to bubble them up
//Synchronization examples var fs = require('fs'); fs.unlinkSync('/tmp/hello'); console.log('successfully deleted /tmp/hello');
Asynchronous methods do not guarantee the order of execution. So the following examples are error prone
fs.rename('/tmp/hello', '/tmp/world', function(err){ if (err) throw err; console.log('renamed complete'); }); fs.stat('/tmp/world', function(err, stats){ if (err) throw err; console.log('stats: ${JSON.stringify(stats)}'); });
fs.stat may be executed before fs.rename. The correct way is to chain callbacks.
fs.rename('/tmp/hello', '/tmp/world', function(err){ if (err) throw err; fs.stat('/tmp/world', function(err, stats){ if (err) throw err; console.log('stats: ${JSON.stringify(stats)}'); }); });
It is recommended that developers use asynchronous versions of these functions. Synchronized versions block the entire process until they complete (stop all connections)
Bottom operation
1. Open the file [fs.open(path, flags[, mode], callback)]
The parameters are as follows:
path <String> | <Buffer> flags <String> | <Number> mode <Integer> Set file mode (permissions and sticky Bit), but only when the file is created. Default is 0666,read-write callback <Function> The callback has two parameters (err error, fd File ID, similar to timer ID)
flags can be:
'r'- Open the file in read mode. An exception occurs if the file does not exist. 'r+'- Open the file in read-write mode. An exception occurs if the file does not exist. 'rs+'- Open the file in synchronous read-write mode. Command the operating system to bypass the local file system cache. 'w'- Open the file in write mode. Files are created (if they do not exist) or truncated (if they exist). 'wx'- similar to'w', but fails if path exists. 'w+'- Open the file in read-write mode. Files are created (if they do not exist) or truncated (if they exist). 'wx+'- Similar to'w+', but fails if path exists. 'a'- Open the file in append mode. If the file does not exist, it will be created. 'ax'- similar to'a', but fails if path exists. 'a+'- Opens the file in read and add mode. If the file does not exist, it will be created. 'ax+'- Similar to'a+', but fails if path exists.
[Note] Using the'rs+'mode does not cause fs.open() to enter a synchronous blocking call. If that's what you want, you should use fs.openSync()
var fs = require('fs'); fs.open('a.txt','r',function(err,fs){ console.log(err);//null console.log(fs);//3 })
var fs = require('fs'); fs.open('b.txt','r',function(err,fs){ /* { Error: ENOENT: no such file or directory, open 'D:\project\b.txt' at Error (native) errno: -4058, code: 'ENOENT', syscall: 'open', path: 'D:\\project\\b.txt' } */ console.log(err); console.log(fs);//undefined })
The second parameter fd in the callback function of a file represents the file identifier, similar to the timer identifier, which identifies the file and increases with the order in which the file is opened.
var fs = require('fs'); fs.open('1.txt','r',function(err,fs){ console.log(fs);//3 }) fs.open('2.txt','r',function(err,fs){ console.log(fs);//4 })
[fs.openSync(path, flags[, mode])]
Synchronized version of fs.open(). Returns an integer representing the file descriptor
var fs = require('fs'); var result = fs.openSync('1.txt','r'); console.log(result);//3
2. Read files [fd, buffer, offset, length, position, callback]
The parameters are as follows:
FD < Integer > file descriptor returned by fs.open() method Buffer < String > | < Buffer > data will be written to buffer Offset to start writing in offset < Integer > buffer Length < Integer > specifies the number of bytes to read (integer) Position < Integer > specifies the position (integer) to read from the file. If position is null, the data is read from the current file location Callback < Function > callback has three parameters (err, bytesRead, buffer). Err is the error message, bytesRead represents the number of bytes read, and buffer is the buffer object
Because the read() method reads the contents of the file into the buffer object, you need to prepare a buffer object in advance.
var fs = require('fs'); fs.open('1.txt','r',function(err,fd){ if(err){ console.log('Failed to open the file'); }else{ var bf = Buffer.alloc(5); fs.read(fd,bf,0,3,null,function(err,len,buffer){ console.log(err);//null console.log(len);//3 console.log(buffer);//<Buffer 61 61 61 00 00> }) } });
[fs.readSync(fd, buffer, offset, length, position)]
Synchronized version of fs.read(), returning the number of bytesRead s
var fs = require('fs'); var fd = fs.openSync('1.txt','r'); var bf = Buffer.alloc(5); var result = fs.readSync(fd,bf,0,3,null); console.log(result);//3
3. Write to file [fs.write(fd, buffer, offset, length[, position], callback)]
The parameters are as follows
FD < Integer > File Identification Buffer < String > | < Buffer > Writes the data in buffer to the file Starting position of data to be written in offset < Integer > buffer object Length < Integer > length is an integer specifying the number of bytes to write Position < Integer > specifies the offset from where data is written from the file. If typeof position!=='number', the data is written from the current location Callback < Function > callback has three parameters (err, write, buffer), where written specifies how many bytes are written from buffer
[Note] It is not safe to use fs.write multiple times on the same file without waiting for callbacks. In this case, fs.createWriteStream is strongly recommended.
When we want to write open files, the mode of opening files should be read-write mode.
var fs = require('fs'); fs.open('1.txt','r+',function(err,fd){ if(err){ console.log('Failed to open the file'); }else{ var bf = Buffer.from('test'); fs.write(fd,bf,0,3,null,function(err,len,buffer){ console.log(err);//null console.log(len);//3 console.log(buffer);//<Buffer 74 65 73 74> }) } });
[fs.write(fd, data[, position[, encoding]], callback)]
This method writes data to the file specified by fd. If data is not a Buffer instance, the value is cast to a string
Unlike writing buffer, the entire string of this method must be written. Substring cannot be specified because the byte offset of the result data may be different from that of the string.
FD < Integer > File Identification Data < String > | < Buffer > Writes data in string or buffer to a file Position < Integer > refers to the offset from where the data is written from the file. If typeof position!=='number', the data is written from the current location Enoding < String > Expected String Encoding Callback < Function > callback has three parameters (err, write, str), where written specifies how many bytes are written from str
var fs = require('fs'); fs.open('1.txt','r+',function(err,fd){ if(err){ console.log('Failed to open the file'); }else{ fs.write(fd,'12345',function(err,len,str){ console.log(err);//null console.log(len);//5 console.log(str);//<Buffer 74 65 73 74> }) } });
[fs.writeSync()]
Synchronized version of fs.write(). Returns the number of bytes written
var fs = require('fs'); var fd = fs.openSync('1.txt','r+'); var bf = Buffer.alloc(5); var result = fs.writeSync(fd,bf,0,3,null); console.log(result);//3
4. Close the file [fs.close(fd, callback)]
Close a file in time after it has been manipulated
The parameters are as follows:
fd - The file descriptor returned by the fs.open() method. callback - callback function, no parameters.
var fs = require('fs'); fs.open('1.txt','r+',function(err,fd){ if(err){ console.log('Failed to open the file'); }else{ fs.close(fd, function(err){ if (err){ console.log(err); } console.log("File Closed Successfully"); }); } });
[fs.closeSync(fd)]
Synchronized version of fs.close(fd, callback), returned undefined
var fs = require('fs'); var fd = fs.openSync('1.txt','r+'); fs.closeSync(fd);
File operation
The previous section describes some of the underlying operations, followed by some more convenient file operations. When using the following methods, you don't need to open and close the file any more, you can do it directly.
1. Write files
[fs.writeFile(file, data[, options], callback)]
Write data asynchronously to a file, create a new file if it does not exist, and replace it if it already exists.
The parameters are as follows:
File - File name or file descriptor. data - data to be written to a file can be String (string) or Buffer (stream) objects. options - This parameter is an object containing {encoding, mode, flag}. The default code is utf8, mode is 0666, flag is'w' callback - callback function, which contains only error information parameters, returns when writing fails.
var fs = require('fs'); var filename = '1.txt'; fs.writeFile(filename,'hello',function(err){ console.log(err);//null })
[fs.writeFileSync(file, data[, options])]
Synchronized version of fs.writeFile(). Return undefined
var fs = require('fs'); var filename = '1.txt'; fs.writeFileSync(filename,'abc');
2. Additional Documents
[fs.appendFile(filename, data, [options], callback)]
Asynchronously append data to a file and create a file if it does not exist. Data can be a string or buffer
The parameters are as follows
File - File name or file descriptor. data - data to be written to a file can be String (string) or Buffer (stream) objects. options - This parameter is an object containing {encoding, mode, flag}. The default code is utf8, mode is 0666, flag is'w' callback - callback function, which contains only error information parameters, returns when writing fails.
var fs = require('fs'); var filename = '1.txt'; fs.appendFile(filename,' world',function(err){ console.log(err);//null })
[fs.appendFileSync(file, data[, options])]
Synchronized version of fs.appendFile(). Return undefined
var fs = require('fs'); var filename = '1.txt'; fs.appendFileSync(filename,' lalala');
3. Reading Files
[fs.readFile(file[, options], callback)]
The parameters are as follows
File - File name or file descriptor options - This parameter is an object containing {encoding, flag}. The default encoding is null, which returns the original buffer if the character encoding is not specified; flag defaults to'r' Callback - Callback function, callback has two parameters (err, data), where data is the content of the file (buffer object), err is the error information parameter, which returns when writing fails.
var fs = require('fs'); var filename = '1.txt'; fs.readFile(filename,function(err,data){ if(err){ console.log('File Reading Failure'); }else{ console.log(data);//<Buffer 61 62 63 20 77 6f 72 6c 64 20 6c 61 6c 61 6c 61> console.log(data.toString());//'abc world lalala' } });
[fs.readFileSync(file[, options])]
Synchronized version of fs.readFile. Returns the contents of the file
If the encoding option is specified, the function returns a string or a buffer
var fs = require('fs'); var filename = '1.txt'; var result = fs.readFileSync(filename); console.log(result);//<Buffer 61 62 63 20 77 6f 72 6c 64 20 6c 61 6c 61 6c 61> console.log(result.toString());'abc world lalala'
4. Delete files
[fs.unlink(path, callback)]
The parameters are as follows:
Path - File path. callback - callback function, no parameters.
var fs = require('fs'); var filename = '1.txt'; fs.unlink(filename, function(err) { if (err) { return console.log('Delete failed'); } console.log("Successful deletion"); });
[fs.unlinkSync(path)]
Synchronized version of fs.unlink(path, callback) with undefined return value
var fs = require('fs'); var filename = '1.txt'; fs.unlink(filename);
5. Rename
[fs.rename(oldPath, newPath, callback)]
The parameters are as follows:
oldPath <String> | <Buffer> newPath <String> | <Buffer> callback <Function> Callback has only one possible exception parameter
var fs = require('fs'); var filename = 'a.txt'; fs.rename(filename,'2.new.txt',function(err){ console.log(err);//null })
[fs.renameSync(oldPath, newPath)]
Synchronized version of fs.rename (old Path, new Path, callback), return undefined
var fs = require('fs'); var filename = '2.new.txt'; var result = fs.renameSync(filename,'a.txt');
6. Document information
[fs.stat(path, callback)]
When fs.stat() executes, an instance of the stats class is returned to its callback function. The related properties of a file can be determined by providing methods in the stats class
The parameters are as follows:
Path - File path. callback - callback function with two parameters such as (err, stats), which is the fs.Stats object
var fs = require('fs'); var filename = 'a.txt'; fs.stat(filename,function(err,stats){ console.log(err);//null /* { dev: 223576, mode: 33206, nlink: 1, uid: 0, gid: 0, rdev: 0, blksize: undefined, ino: 7599824371527537, size: 0, blocks: undefined, atime: 2017-06-03T14:18:15.370Z, mtime: 2017-06-03T14:18:15.370Z, ctime: 2017-06-03T16:32:05.776Z, birthtime: 2017-06-03T14:18:15.370Z } */ console.log(stats); });
The methods in the stats class are
stats.isFile() returns true if the file is true, otherwise false. stats.isDirectory() returns true if the directory, otherwise false. stats.isBlockDevice() returns true if it is a block device, otherwise false. stats.isCharacterDevice() returns true if it is a character device, otherwise false. stats.isSymbolicLink() returns true if it is a soft link, otherwise false. stats.isFIFO() returns true if it is FIFO, or false if it is not. FIFO is a special type of command pipeline in UNIX. stats.isSocket() returns true if it is Socket, otherwise false.
var fs = require('fs'); var filename = 'a.txt'; fs.stat(filename,function(err,stats){ console.log(stats.isFile());//true });
[fs.statSync(path)]
Synchronized version of fs.stat(path, callback) method, returning an instance of fs.Stats
var fs = require('fs'); var filename = 'a.txt'; var result = fs.statSync(filename); /* { dev: 223576, mode: 33206, nlink: 1, uid: 0, gid: 0, rdev: 0, blksize: undefined, ino: 7599824371527537, size: 0, blocks: undefined, atime: 2017-06-03T14:18:15.370Z, mtime: 2017-06-03T14:18:15.370Z, ctime: 2017-06-03T16:32:05.776Z, birthtime: 2017-06-03T14:18:15.370Z } */ console.log(result);
7. Monitoring
[fs.watch(filename[, options][, listener])]
This method is used to monitor changes in filename, which can be a file or a directory. The object returned is an fs.FSWatcher
The parameters are as follows
filename <String> | <Buffer> options <String> | <Object> The parameters are optional if options Is a string, then it specifies encoding. otherwise options Should be passed in as an object persistent <Boolean> Indicates whether the process should continue to run if the file is being monitored. Default is true recursive <Boolean> Indicates whether all subdirectories should be monitored, or only the current directory. Suitable for platforms that are supported only when a directory is specified. Default is false encoding <String> Specifies the character encoding for the file name to be passed to the listener. Default is'utf8' listener <Function> The callback function has two parameters (eventType, filename). eventType It could be'rename'or'change',filename Is the name of the file that triggered the event
Filename parameters provided in callbacks are only supported on Linux and Windows systems. Even on supported platforms, filename is not guaranteed. Therefore, don't assume that the filename parameter is always provided in the callback. If it's empty, there needs to be some backup logic.
fs.watch('somedir', (eventType, filename) => { console.log(`The event type is: ${eventType}`); if (filename) { console.log(`File name provided: ${filename}`); } else { console.log('Document name not provided'); } });
var fs = require('fs'); var filename = '1.txt'; fs.watch(filename,function(eventType, _filename){ console.log(eventType);//change if(_filename){ console.log(_filename + 'Change has taken place.');//'1.txt Change has taken place.' }else{ console.log('...'); } })
[Note] When a file appears or disappears in a directory,'rename'will also be triggered
Directory operation
1. Creation
[fs.mkdir(path[, mode], callback)]
The parameters are as follows:
Path - File path. mode - Sets directory permissions by default to 0777. Callback - callback function, callback has only one possible exception parameter
var fs = require('fs'); fs.mkdir('./1',function(err){ console.log(err);//null })
[fs.mkdirSync(path[, mode])]
Synchronized version of fs.mkdir(path[, mode], callback), returned undefined
var fs = require('fs'); fs.mkdirSync('./2');
2. Delete
[fs.rmdir(path, callback)]
The parameters are as follows:
Path - File path. Callback - callback function, callback has only one possible exception parameter
var fs = require('fs'); fs.rmdir('./1',function(err){ console.log(err);//null })
[fs.rmdirSync(path, callback)]
Synchronized version of fs.rmdir(path, callback), returned undefined
var fs = require('fs'); fs.rmdirSync('./2');
3. Read
[fs.readdir(path[, options], callback)]
The parameters are as follows:
path <String> | <Buffer> options <String> | <Object> Optional options A parameter is used to pass in a callback file name, which can be a string and specify a character encoding, or an object with one encoding Property specifies the character encoding to be used. If encoding Set as 'buffer',The returned file name will be treated as Buffer Object Input encoding <String> default = 'utf8' callback <Function> The callback has two parameters (err, files),among files Is not included in the catalog '.' and '..' An array of file names
var fs = require('fs'); fs.readdir('./',function(err,data){ console.log(err);//null /* [ '.csslintrc', '.jshintrc', 'a.txt', 'dist', 'Gruntfile.js', 'Gruntfile1.js', 'index.html', 'main.js', 'node_modules', 'package.json', 'src' ] */ console.log(data); })
var fs = require('fs'); fs.readdir('./',function(err,data){ data.forEach(function(item,index,arr){ fs.stat(item,function(err,stats){ if(stats.isFile()){ console.log('Documents:' + item); } if(stats.isDirectory()){ console.log('Catalog:' + item); } }); }) }) /* Document:.jshintrc Document:.csslintrc Directory: dist File: Gruntfile.js Document: index.html File: Gruntfile1.js Document: main.js Directory: node_modules File: package.json Document: a.txt Directory: src */
[fs.readdirSync(path[, options], callback)]
Synchronized version of fs.readdir(path[, options], callback) returns an array of filenames that do not include'. 'and'.'
var fs = require('fs'); var result = fs.readdirSync('./'); /* [ '.csslintrc', '.jshintrc', 'a.txt', 'dist', 'Gruntfile.js', 'Gruntfile1.js', 'index.html', 'main.js', 'node_modules', 'package.json', 'src' ] */ console.log(result);