fs.readFileSync(path[,
options])
This
method read the contents of the file synchronously.
Below
table summarizes the arguments of readFileSync method.
Argument
|
Type
|
Description
|
|||||||||
path
|
string
or Buffer or URL or integer
|
Specifies
the filename of file descriptor.
|
|||||||||
options
|
Object
or string
|
Below
table summarizes different options supported by readFileSync method.
|
Find
the below working application.
Let
‘input.txt’ contains below information.
Two roads diverged in a yellow wood, Jb_modern_frost_2_eAnd sorry I could not travel both And be one traveler, long I stood And looked down one as far as I could To where it bent in the undergrowth;
HelloWorld.js
var fs = require('fs'); var filePath = 'input.txt'; var contents = fs.readFileSync(filePath, 'UTF-8'); console.log(contents); console.log('Done.......')
Output
Two roads diverged in a yellow wood, Jb_modern_frost_2_eAnd sorry I could not travel both And be one traveler, long I stood And looked down one as far as I could To where it bent in the undergrowth; Done.......
If
you do not specify encoding, ‘readFileSync’ method returns a buffer.
var fs = require('fs'); var filePath = 'input.txt'; var contents = fs.readFileSync(filePath); console.log(contents); console.log('Done.......')
No comments:
Post a Comment