nodeschool.io 3
2013-12-04 18:12
429 查看
~~ MY FIRST I/O! ~~
Write a program that uses a single synchronous filesystem operation to
read a file and print the number of newlines it contains to the
console (stdout), similar to running `cat file | wc -l`.
The full path to the file to read will be provided as the first
command-line argument.
----------------------------------------------------------------------
HINTS:
To perform a filesystem operation you are going to need the `fs`
module from the Node core library. To load this kind of module, or any
other "global" module, use the following incantation:
var fs = require('fs')
Now you have the full `fs` module available in a variable named `fs`.
All synchronous (or blocking) filesystem methods in the `fs` module
end with 'Sync'. To read a file, you'll need to use
`fs.readFileSync('/path/to/file')`. This method will return a
`Buffer` object containing the complete contents of the file.
Documentation on the `fs` module can be found by pointing your browser
here:
C:\Users\dzhang\AppData\Roaming\npm\node_modules\learnyounode\node_apidoc\fs.h
tml
`Buffer` objects are Node's way of efficiently representing arbitrary
arrays of data, whether it be ascii, binary or some other format.
`Buffer` objects can be converted to strings by simply calling the
`toString()` method on them. e.g. `var str = buf.toString()`.
Documentation on `Buffer`s can be found by pointing your browser here:
C:\Users\dzhang\AppData\Roaming\npm\node_modules\learnyounode\node_apidoc\buff
er.html
If you're looking for an easy way to count the number of newlines in a
string, recall that a JavaScript `String` can be `.split()` into an
array of substrings and that '\n'. Using this method you'll end up
with an array that has one more element than the number of newlines.
Write a program that uses a single synchronous filesystem operation to
read a file and print the number of newlines it contains to the
console (stdout), similar to running `cat file | wc -l`.
The full path to the file to read will be provided as the first
command-line argument.
----------------------------------------------------------------------
HINTS:
To perform a filesystem operation you are going to need the `fs`
module from the Node core library. To load this kind of module, or any
other "global" module, use the following incantation:
var fs = require('fs')
Now you have the full `fs` module available in a variable named `fs`.
All synchronous (or blocking) filesystem methods in the `fs` module
end with 'Sync'. To read a file, you'll need to use
`fs.readFileSync('/path/to/file')`. This method will return a
`Buffer` object containing the complete contents of the file.
Documentation on the `fs` module can be found by pointing your browser
here:
C:\Users\dzhang\AppData\Roaming\npm\node_modules\learnyounode\node_apidoc\fs.h
tml
`Buffer` objects are Node's way of efficiently representing arbitrary
arrays of data, whether it be ascii, binary or some other format.
`Buffer` objects can be converted to strings by simply calling the
`toString()` method on them. e.g. `var str = buf.toString()`.
Documentation on `Buffer`s can be found by pointing your browser here:
C:\Users\dzhang\AppData\Roaming\npm\node_modules\learnyounode\node_apidoc\buff
er.html
If you're looking for an easy way to count the number of newlines in a
string, recall that a JavaScript `String` can be `.split()` into an
array of substrings and that '\n'. Using this method you'll end up
with an array that has one more element than the number of newlines.
var fs = require('fs'); var arr = fs.readFileSync(process.argv[2],'utf-8').split("\n"); console.log(arr.length-1);
相关文章推荐
- nodeschool.io 9
- nodeschool.io 2
- nodeschool.io 6
- nodeschool.io 10
- nodeschool.io 4
- nodeschool.io 7
- nodeschool.io 5
- 五子棋online---React.js+Socket.io+Node.js
- socket.io (nodejs+express)
- Node.js下基于Express + Socket.io 搭建一个基本的在线聊天室
- node.js+block.io+redis,聊天室
- Node.js 的异步 IO 性能探讨
- Node.js socket.io前端后端通讯简单例子
- TWaver HTML5 + Node.js + express + socket.io + redis(五)
- NodeJS+Express+Socket.io的简单及时通讯
- How to pass the paramater of Node.js or io.js into native C/C++ function.
- node.js之socket.io模块
- nodejs+socket.io实现websocket通信初探
- node.js利用socket.io实现多人在线匹配联机五子棋
- 聊天室入门实战(node,socket.io实现)--第一章(实现登录群聊功能)