0
fs cheatsheet
source link: https://devhints.io/nodejs-fs
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Reading
fs.readFile('file.txt', function(err, data) { .. });
fs.readFile('file.txt', {encoding: 'utf-8'}, function(err, data) { .. });
Writing
fs.writeFile('output.txt', function(err) { .. });
fs.appendFile('output.txt', function(err) { .. });
Watch
fs.watch('dir OR file.txt', { persistent: true }, function(event, file) {
event; /* rename | change */
});
GettingĀ info
fs.exists('file.txt', function(exists /*bool*/) { ... });
fs.stat('file.txt', function(stats) {
stats.isFile();
stats.isDirectory();
stats.isSymbolicLink();
});
FileĀ operations
fs.rename('old.txt', 'new.txt', function(){});
fs.chown('file.txt', uid, gid, function(){});
fs.symlink('src', 'dest', function(){});
fs.unlink('path', function(){});
fs.rmdir('path', function(){});
fs.readdir('path', function(err, files) { .. }); /* `files` = array of names */
fs.realpath('/etc/passwd', function(err, path) { /* "/private/etc/passwd" */ });
data = fs.readFileSync('input.txt');
fs.writeFileSync('output.txt', data);
fs.appendFileSync('output.txt', data);
fs.existsSync('file.txt');
References
- http://nodejs.org/api/fs.html
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK