0%

learnyounode Lesson 3

课程目标:

1
2
3
Write a program that uses a single synchronous filesystem operation to
read a file and print the number of newlines (\n) it contains to the
console (stdout), similar to running cat file | wc -l.

My Solution:

1
2
3
4
5
6
7
8
9
10
const fs = require("fs");
const buffer = fs.readFileSync(process.argv[2]);

const result = buffer.toString().split("\n").length - 1;
console.log(result);

// 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

这节课主要讲的 fs 的同步读取文件方法readFileSync,第一个参数是文件路径,第二个参数是编码格式。返回的是node特有的buffer格式,可以通过不同方法转换所需要的类型(eg.toString())

好了认识了第一个 node 核心模块(core modules)fs.过关!