Api of Node.js File System
//Public citation
var fs = require('fs'),
path = require('path');
1. readFile function for reading files
//readFile(filename,[options],callback);
/**
* filename, Required parameter, file name
* [options],Optional parameters, can specify flag (file operation options, such as r + read and write; w + read and write, if the file does not exist, create) and encoding attributes
* callback After reading the file, the callback function defaults to the first err and the second data.
*/
fs.readFile(__dirname + '/test.txt', {flag: 'r+', encoding: 'utf8'}, function (err, data) {
if(err) {
console.error(err);
return;
}
console.log(data);
});
2. Writing Documents
// fs.writeFile(filename,data,[options],callback);
var w_data = 'This is a passage through. fs.writeFile The content written by the function;\r\n';
var w_data = new Buffer(w_data);
/**
* filename, Required parameter, file name
* data, Written data, which can be a character or a Buffer object
* [options],flag,mode(Permission), encoding
* callback After reading the file, the callback function defaults to the first err and the second data.
*/
fs.writeFile(__dirname + '/test.txt', w_data, {flag: 'a'}, function (err) {
if(err) {
console.error(err);
} else {
console.log('Write successfully');
}
});
3. Writing Documents Additionally
// fs.appendFile(filename,data,[options],callback);
fs.appendFile(__dirname + '/test.txt', 'Use fs.appendFile Additional document content', function () {
console.log('Additional Content Completion');
});
4. Opening Files
// fs.open(filename, flags, [mode], callback);
/**
* filename, Required parameter, file name
* flags, Operational identification, such as "r", opens in read mode
* [mode],Permissions, such as 777, indicate that any user can read and write to execute
* callback Callback function after opening the file. The parameter defaults to the first err and the second fd is an integer, which indicates the file descriptor returned by opening the file. It is also called file handle in window s.
*/
fs.open(__dirname + '/test.txt', 'r', '0666', function (err, fd) {
console.log(fd);
});
5. Read the file and read the contents of the opened file into the buffer.
//fs.read(fd, buffer, offset, length, position, callback);
/**
* fd, Open the file descriptor returned after success using fs.open
* buffer, A Buffer object, a segment of memory allocated by the v8 engine
* offset, Integer, the initial position of writing to the buffer in bytes
* length, Integer, read file length
* position, Integer to read the initial location of the file; file size in bytes
* callback(err, bytesRead, buffer), Callback function after read execution is completed, bytesRead actually reads bytes, and the buffer object read
*/
fs.open(__dirname + '/test.txt', 'r', function (err, fd) {
if(err) {
console.error(err);
return;
} else {
var buffer = new Buffer(255);
console.log(buffer.length);
//Each utf8 encoding of Chinese character is 3 bytes, and English is 1 byte.
fs.read(fd, buffer, 0, 9, 3, function (err, bytesRead, buffer) {
if(err) {
throw err;
} else {
console.log(bytesRead);
console.log(buffer.slice(0, bytesRead).toString());
//After reading, when using fd to read, the base point is calculated based on the last reading position.
fs.read(fd, buffer, 0, 9, null, function (err, bytesRead, buffer) {
console.log(bytesRead);
console.log(buffer.slice(0, bytesRead).toString());
});
}
});
}
});
6. Write files to write data in the buffer to files opened with fs.open
//fs.write(fd, buffer, offset, length, position, callback);
/**
* fd, Open the file descriptor returned after success using fs.open
* buffer, A Buffer object, a segment of memory allocated by the v8 engine
* offset, Integer, the initial position read from the buffer, in bytes
* length, Integer, the number of bytes read from the buffer
* position, Integer, write to the initial location of the file;
* callback(err, written, buffer), Callback function after completion of write operation, actual number of bytes written by written, buffer object read by buffer
*/
fs.open(__dirname + '/test.txt', 'a', function (err, fd) {
if(err) {
console.error(err);
return;
} else {
var buffer = new Buffer('Write file data content');
//Write in the word'File in'.
fs.write(fd, buffer, 3, 9, 12, function (err, written, buffer) {
if(err) {
console.log('fail to write to file');
console.error(err);
return;
} else {
console.log(buffer.toString());
//Write the word'In data'.
fs.write(fd, buffer, 12, 9, null, function (err, written, buffer) {
console.log(buffer.toString());
})
}
});
}
});
7. Refresh the buffer;
// When writing to a file using fs.write, the operating system reads the data into memory and then writes the data into the file. When the data is read out, it does not mean that the data has been written, because some of them may also be in the internal buffer.
// Therefore, we can use fs.fsync method to write data in memory to files; - refresh memory buffer;
//fs.fsync(fd, [callback])
/**
* fd, Open the file descriptor returned after success using fs.open
* [callback(err, written, buffer)], Callback function after completion of write operation, actual number of bytes written by written, buffer object read by buffer
*/
fs.open(__dirname + '/test.txt', 'a', function (err, fd) {
if(err)
throw err;
var buffer = new Buffer('I love nodejs programming');
fs.write(fd, buffer, 0, 9, 0, function (err, written, buffer) {
console.log(written.toString());
fs.write(fd, buffer, 9, buffer.length - 9, null, function (err, written) {
console.log(written.toString());
fs.fsync(fd);
fs.close(fd);
})
});
});
8. Create directories;
//Create directories using fs.mkdir
//fs.mkdir(path, [mode], callback);
/**
* path, The complete path and directory name of the created directory;
* [mode], Directory permissions, default 0777
* [callback(err)], Create a directory callback function, err error object
*/
fs.mkdir(__dirname + '/fsDir', function (err) {
if(err)
throw err;
console.log('Successful directory creation')
});
9. Read directories;
//Use fs.readdir to read the directory, focusing on the files object in its callback function
//fs.readdir(path, callback);
/**
* path, To read the full path and directory name of the directory;
* [callback(err, files)], Read the directory callback function; err error object, files array, store all file names in the read directory
*/
fs.readdir(__dirname + '/fsDir/', function (err, files) {
if(err) {
console.error(err);
return;
} else {
files.forEach(function (file) {
var filePath = path.normalize(__dirname + '/fsDir/' + file);
fs.stat(filePath, function (err, stat) {
if(stat.isFile()) {
console.log(filePath + ' is: ' + 'file');
}
if(stat.isDirectory()) {
console.log(filePath + ' is: ' + 'dir');
}
});
});
for (var i = 0; i < files.length; i++) {
//Using closures does not guarantee that the order of reading files is the same as that saved in the array.
(function () {
var filePath = path.normalize(__dirname + '/fsDir/' + files[i]);
fs.stat(filePath, function (err, stat) {
if(stat.isFile()) {
console.log(filePath + ' is: ' + 'file');
}
if(stat.isDirectory()) {
console.log(filePath + ' is: ' + 'dir');
}
});
})();
}
}
});
Here is a supplement:
stat
If we want to get information about file size, creation time, etc., we can use fs.stat(), which returns a Stat object and can tell us the details of the file or directory:
'use strict'; var fs = require('fs'); fs.stat('sample.txt', function (err, stat) { if (err) { console.log(err); } else { // Is it a document? console.log('isFile: ' + stat.isFile()); // Is it a directory? console.log('isDirectory: ' + stat.isDirectory()); if (stat.isFile()) { // File size: console.log('size: ' + stat.size); // Creation time, Date object: console.log('birth time: ' + stat.birthtime); // Modification time, Date object: console.log('modified time: ' + stat.mtime); } } });
10. View the information of files and directories;
//fs.stat(path, callback);
//fs.lstat(path, callback); // View symbolic link file
/**
* path, To view the full path and name of the directory/file;
* [callback(err, stats)], Callback function for operation completion; err error object, stat fs.Stat, an object instance, providing such attributes as isFile, isDirectory,isBlockDevice and size,ctime,mtime, etc.
*/
//Examples to see fs.readdir
11. Check whether files and directories exist
//fs.exists(path, callback);
/**
* path, To view the full path and name of the directory/file;
* [callback(exists)], Callback function for operation completion; exists true exists, false indicates nonexistence
*/
fs.exists(__dirname + '/te', function (exists) {
var retTxt = exists ? retTxt = 'Document Existence' : 'file does not exist';
console.log(retTxt);
});
12. Modification of file access time and modification time
//fs.utimes(path, atime, mtime, callback);
/**
* path, To view the full path and name of the directory/file;
* atime, New access time
* ctime, New modification time
* [callback(err)], Callback function for operation completion; object for err operation failure
*/
fs.utimes(__dirname + '/test.txt', new Date(), new Date(), function (err) {
if(err) {
console.error(err);
return;
}
fs.stat(__dirname + '/test.txt', function (err, stat) {
console.log('Access time: ' + stat.atime.toString() + '; \n Modification time:' + stat.mtime);
console.log(stat.mode);
})
});
13. Operational permissions to modify files or directories
//fs.utimes(path, mode, callback);
/**
* path, To view the full path and name of the directory/file;
* mode, Specify permissions, such as: 06668, permissions: all users can read and write,
* [callback(err)], Callback function for operation completion; object for err operation failure
*/
fs.chmod(__dirname + '/fsDir', 0666, function (err) {
if(err) {
console.error(err);
return;
}
console.log('Successful modification of permissions')
});
14. Move/rename files or directories
//fs.rename(oldPath, newPath, callback);
/**
* oldPath, The full path and name of the original directory/file;
* newPath, The full path and name of the new directory/file; if the new path is the same as the original path and only the file name is different, it is renamed
* [callback(err)], Callback function for operation completion; object for err operation failure
*/
fs.rename(__dirname + '/test', __dirname + '/fsDir', function (err) {
if(err) {
console.error(err);
return;
}
console.log('Successful renaming')
});
15. Delete empty directories
//fs.rmdir(path, callback);
/**
* path, The complete path and directory name of the directory;
* [callback(err)], Callback function for operation completion; object for err operation failure
*/
fs.rmdir(__dirname + '/test', function (err) {
fs.mkdir(__dirname + '/test', 0666, function (err) {
console.log('Establish test Catalog');
});
if(err) {
console.log('Failure to delete empty directories, the possible reasons: 1. directory does not exist, 2. directory is not empty')
console.error(err);
return;
}
console.log('Successful deletion of empty directories!');
});
16. Surveillance Documents
//Monitor files and perform processing when the files are modified
//fs.watchFile(filename, [options], listener);
/**
* filename, Full path and file name;
* [options], persistent true Represents continuous monitoring without exiting the program; interval unit milliseconds, which indicates how many milliseconds every file is monitored
* listener, Callback when a file changes has two parameters: curr is an fs.Stat object, the modified file, prev, an fs.Stat object, representing the modified object.
*/
fs.watchFile(__dirname + '/test.txt', {interval: 20}, function (curr, prev) {
if(Date.parse(prev.ctime) == 0) {
console.log('Files are created!');
} else if(Date.parse(curr.ctime) == 0) {
console.log('File deleted!')
} else if(Date.parse(curr.mtime) != Date.parse(prev.mtime)) {
console.log('Document modified');
}
});
fs.watchFile(__dirname + '/test.txt', function (curr, prev) {
console.log('This is the second one. watch,Monitor file changes');
});
17. Cancellation of surveillance files
//Cancel file monitoring
//fs.unwatchFile(filename, [listener]);
/**
* filename, Full path and file name;
* [listener], To cancel listener events, if not specified, cancel all listener processing events
*/
var listener = function (curr, prev) {
console.log('I'm a monitor function.')
}
fs.unwatchFile(__dirname + '/test.txt', listener);
18. Monitoring files or directories
// Monitor files or directories and perform processing when changes are monitored.
// fs.watch returns an fs.FSWatcher object with a close method to stop the watch operation.
// When a file changes in fs.watch, it triggers the change(err, filename) event of the fs.FSWatcher object, the err error object, and the filename changed file name.
// fs.watch(filename, [options], [listener]);
/**
* filename, Full path and file name or directory name;
* [listener(event, filename], The listener event has two parameters: event represents rename for rename for rename for rename for rename for rename for rename for rename, rename for rename for rename, deletion or movement, or change for modification, and filename for changed file path
*/
var fsWatcher = fs.watch(__dirname + '/test', function (event, filename) {
//console.log(event)
});
//console.log(fsWatcher instanceof FSWatcher);
fsWatcher.on('change', function (event, filename) {
console.log(filename + ' undergo changes')
});
//Close surveillance after 30 seconds
setTimeout(function () {
console.log('Close')
fsWatcher.close(function (err) {
if(err) {
console.error(err)
}
console.log('Close watch')
});
}, 30000);
19. File Stream
/* * Stream, which represents a set of orderly, starting and ending byte data transmission means in an application program. * Node.js implements the object of stream.Readable/stream.Writeable interface to read and write stream data; the above interfaces are inherited from EventEmitter class, so different events are triggered when the read/write stream is in different states. * About stream reading: Node.js constantly reads a small piece of file content into the buffer, and then reads the content from the buffer; * About stream writing: Node.js continuously writes stream data to the internal buffer, and then writes the buffer to the file when the buffer is full; repeat the above operation until the content is written; * readFile, read, writeFile and write all put the whole file into memory and operate again, but they are part of the file data and part of the data operation. * * Stream reading - ----------------------------------------------------------------------------------------------------------------- * Read data objects: * ReadStream reads files * http.IncomingMessage Client Request or Server Response * net.Socket Socket Port Object * Standard Output of child.stdout Subprocess * child.stdin subprocess standard entry * process.stdin is used to create process standard input streams * Gzip, Deflate, DeflateRaw Data Compression * * Trigger events: * readable Data readable Time * After data data is read * When the end data reading is complete * error Data Reading error * close closes a stream object * * Object operation method for reading data: * read Reading Data Method * setEncoding Sets the Editing of Read Data * pause notifies the audience to stop triggering data events * resume notification object recovery triggers data events * pipe sets up the data channel to access the read-in data into the write stream. * unpipe Cancel Channel * unshift When stream data binds to a parser, this method cancels the parser * * Stream Writing - --------------------------------------------------------------------------------------------------------------------------------------------------------- * Write data objects: * WriteStream Writes File Objects * http.clientRequest writes HTTP client request data * http.ServerResponse writes HTTP server-side response data * net.Socket reads and writes TCP or UNIX streams, requiring connection events to be passed to users * Standard Output of child.stdout Subprocess * child.stdin subprocess standard entry * Gzip, Deflate, DeflateRaw Data Compression * * Write data trigger event: * drain When the write method returns false, it indicates that the buffer has been output to the target object and can continue to write data to the buffer. * finish When the end method is called, all data is written. * pipe when the pipe method of the object used to read data is called * unpipe when the unpipe method is called * Error when an error occurs * * Write data method: * Write is used to write data * end ends writing, and then writing will cause an error. */
20. Create a read stream
//fs.createReadStream(path, [options])
/**
* path File path
* [options] flags:Specify file operation, default'r', read operation; encoding, specify read stream coding; autoClose, whether to close automatically after reading is completed, default true; start specifies file start reading position; end specifies file start reading end position
*/
var rs = fs.createReadStream(__dirname + '/test.txt', {start: 0, end: 2});
//open is an event in the ReadStream object that represents the opening of a file.
rs.on('open', function (fd) {
console.log('Start reading files');
});
rs.on('data', function (data) {
console.log(data.toString());
});
rs.on('end', function () {
console.log('End of Reading File')
});
rs.on('close', function () {
console.log('File closure');
});
rs.on('error', function (err) {
console.error(err);
});
//Pause and reply file reading;
rs.on('open', function () {
console.log('Start reading files');
});
rs.pause();
rs.on('data', function (data) {
console.log(data.toString());
});
setTimeout(function () {
rs.resume();
}, 2000);
21. Create a write stream
//fs.createWriteStream(path, [options])
/**
* path File path
* [options] flags:Specify file operation, default'w',; encoding, specify read stream encoding; start specifies where to write to the file
*/
/* ws.write(chunk, [encoding], [callback]);
* chunk, You can write data for a Buffer object or a string
* [encoding], Code
* [callback], Write-back callback
*/
/* ws.end([chunk], [encoding], [callback]);
* [chunk], Data to be written
* [encoding], Code
* [callback], Write-back callback
*/
var ws = fs.createWriteStream(__dirname + '/test.txt', {start: 0});
var buffer = new Buffer('I like you, too');
ws.write(buffer, 'utf8', function (err, buffer) {
console.log(arguments);
console.log('Write is complete, callback function has no parameters')
});
//The last thing to write
ws.end('Bye');
//Using Stream to Complete File Replication
var rs = fs.createReadStream(__dirname + '/test.txt')
var ws = fs.createWriteStream(__dirname + '/test/test.txt');
rs.on('data', function (data) {
ws.write(data)
});
ws.on('open', function (fd) {
console.log('The data file to be written has been opened, and the file descriptor is: ' + fd);
});
rs.on('end', function () {
console.log('File Reading Completed');
ws.end('complete', function () {
console.log('All files are written to completion')
});
});
//For WriteStream objects, the write method returns a Boolean type and false when the data in the buffer is full.
//Represents that the buffer is full and will be immediately exported to the target object
//First example
var ws = fs.createWriteStream(__dirname + '/test/test.txt');
for (var i = 0; i < 10000; i++) {
var w_flag = ws.write(i.toString());
//When the buffer is full, output false
console.log(w_flag);
}
//Second example
var ws = fs.createWriteStream(__dirname + '/test/untiyou.mp3');
var rs = fs.createReadStream(__dirname + '/test/Until You.mp3');
rs.on('data', function (data) {
var flag = ws.write(data);
console.log(flag);
});
//The system buffer data has all output triggering drain event
ws.on('drain', function () {
console.log('The system buffer data has all been output.')
});
22. Pipeline Realizes Stream Reading and Writing
//rs.pipe(destination, [options]);
/**
* destination Must be a writable stream data object
* [opations] end The default is true, which means to close the file immediately after reading.
*/
var rs = fs.createReadStream(__dirname + '/test/Until You.mp3');
var ws = fs.createWriteStream(__dirname + '/test/untiyou.mp3');
rs.pipe(ws);
rs.on('data', function (data) {
console.log('Data readable')
});
rs.on('end', function () {
console.log('File Reading Completed');
//ws.end('Goodbye')
});
Wen/Mingming Provinces (Brief Book Author)
Links to the original text: http://www.jianshu.com/p/5683c8a93511
Links to the original text: http://www.jianshu.com/p/5683c8a93511