Skip to content

Instantly share code, notes, and snippets.

@toanalien
Created June 4, 2015 17:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save toanalien/73b791a64c60e34df3b2 to your computer and use it in GitHub Desktop.
Save toanalien/73b791a64c60e34df3b2 to your computer and use it in GitHub Desktop.
The fs module
/**
* Querying file statistics
* You can query some meta-infomation on a file (or directory) by using the fs.stat function
*/
var fs = require('fs');
fs.stat('/etc/paswwd', function(err, stats){
if (err) { throw err;}
console.log(stats);
});
/**
* ouput like the following:
* { dev: 234881026,
* ino: 95028917,
* mode: 33188, n
* link: 1,
* uid: 0,
* gid: 0,
* rdev: 0,
* size: 5086,
* blksize: 4096,
* blocks: 0,
* atime: Fri, 18 Nov 2011 22:44:47 GMT,
* mtime: Thu, 08 Sep 2011 23:50:04 GMT,
* ctime: Thu, 08 Sep 2011 23:50:04 GMT }
*/
// The fs.stat() function call passes an instance of the stats class into the callback function,
// which you can use to call any of the following:
stats.isFile();
stats.isDirectory();
stats.isBlockDevice();
stats.isCharacterDevice();
stats.isSymbolicLink();
stats.isFifo();
stats.isSocket();
/**
* Opening a file
*/
var fs = require('fs');
fs.open('/path/to/file', 'r', function(err, fd){
// got fd file descriptor
});
/**
* The first argument to fs.open() is the file path.
* The second argument contains the flags, which indicate the mode with which the file shuld open.
* The flags can be r, r+, w, w+, a or a+
*
* r - Opens the text file for reading. The stream is positioned at the begining of the file.
* r+ - Opens the text for reading and writing. The stream is positioned at the beginning of the file.
* w - Truncates the file to zero length or creates a text file for writing. The stream í positioned at the beginning of the file.
* w+ - Opens the file for reading and writing. The file is created if it does not exist. Otherwise it is truncated. The stream is positioned at the begining of the file.
* a - Opens the file for writing. The file is created if is does not exist. The stream is positioned at the end of the file.
* a+ - Opens the file for reading and writing. The file is created if is does not exist. The stream is positioned at the end of the file.
/**
* Reading from a file
* Once a file is open, you can read a part of it, but before you do so, you have to create a buffer that will contain the data.
* that buffer will be passed to the fs.read function to be filled with the file data
*/
var fs = require('fs');
fs.open = ('./my_file.txt', 'r', function opened(err, fd){
if (err) { throw err; }
var readBuffer = new Buffer(1024),
bufferOffset = 0,
bufferLength = readBuffer(length),
fileLength = readBuffer.length,
filePossition = 100;
fs.read(fs,
readBuffer,
bufferOffset,
bufferLength,
filePossition,
function read(err, readBytes){
if(err) { throw err; }
console.log('just read ' + readBytes + 'bytes');
if (readByte>0){
if (err) { throw err; }
console.log(readBuffer.slice(0, readBytes));
}
});
});
/**
* Writing to a file
*/
var fs = require('fs');
fs.open('./my_file.txt', 'a', function opened(err, fd){
if (err) { throw err; }
var writeBuffer = new Buffer('writing the string'),
bufferPosition = 0,
bufferLength = writeBuffer.length,
filePosition = null;
fs.write(fd,
writeBuffer,
bufferPosition,
bufferLength,
filePostition,
function wrote(err, written){
if (err) { throw err; }
console.log('wrote ' + written + ' bytes');
});
});
/**
* Closing a file
*/
// It’s quite easy to leak fi le descriptors if you’re not careful.
// Next is an example that provides a function named openAndWriteToSystemLog with careful file closing
var fs = require('fs');
function openAndWriteToSystemLog(writeBuffer, callback) {
fs.open('./my_file', 'a', function opened(err, fd) {
if (err) { return callback(err); }
function notifyError(err) {
fs.close(fd, function() {
callback(err);
});
}
var bufferOffset = 0,
bufferLength = writeBuffer.length,
filePosition = null;
fs.write( fd, writeBuffer, bufferOffset, bufferLength, filePosition,
function wrote(err, written) {
if (err) { return notifyError(err); }
fs.close(fd, function() {
callback(err);
});
}
);
});
}
openAndWriteToSystemLog(
new Buffer('writing this string'),
function done(err) {
if (err) {
console.log("error while opening and writing:", err.message);
return;
}
console.log('All done with no errors');
}
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment