Created
February 17, 2016 22:25
-
-
Save greemwahr/99b1e0196124bbfa97fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Process I/O files synchronously in node.js | |
var fs = require('fs') | |
var contents = fs.readFileSync(process.argv[2]) | |
var lines = contents.toString().split('\n').length - 1 | |
console.log(lines) | |
// note you can avoid the .toString() by passing 'utf8' as the | |
// second argument to readFileSync, then you'll get a String! | |
// | |
// fs.readFileSync(process.argv[2], 'utf8').split('\n').length - 1 | |
# Process I/O files asynchronously in node.js | |
var fs = require('fs') | |
var file = process.argv[2] | |
fs.readFile(file, function (err, contents) { | |
// fs.readFile(file, 'utf8', callback) can also be used | |
var lines = contents.toString().split('\n').length - 1 | |
console.log(lines) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment