Created
November 7, 2016 11:25
-
-
Save akshayvinchurkar/e272e801fbbe0a25a5fe975d72f39236 to your computer and use it in GitHub Desktop.
Read File From Node script
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
var fs = require('fs'); | |
//var filename = ''; | |
var prompt = require('prompt'); | |
// make a simple validation for filename | |
var filename = { | |
properties: { | |
filename: { | |
patturn: /^[a-zA-Z\s\-]+./, | |
message: 'Filename', | |
required:true | |
} | |
} | |
}; | |
function readmyfile(filename) { | |
// here i simply read the file | |
fs.readFile(filename, 'utf8', function(err, result){ | |
// if any error comes in in reading file | |
if (err) { | |
console.log('file error'); | |
} else { | |
console.log("\n" + result); | |
/* now i have that reading data in result variable i simply split that in to array | |
so bacically it just convert that data in to array from here this optional code i just experiment*/ | |
var wordsarray = result.split(''); | |
// i need to stringify that array to make it writable in to file | |
var perfectarray = JSON.stringify(wordsarray); | |
// now simply write that array into file simple? | |
fs.writeFile('new.txt', perfectarray, 'utf8', function(err, result) { | |
if (err) { | |
console.log('this is file error'); | |
} | |
console.log('Success'); | |
}); | |
} | |
}); | |
} | |
prompt.start(); | |
prompt.get(filename, function(err, result){ | |
readmyfile(result.filename); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
its almost like cat command in linux just read the file