Maison >interface Web >tutoriel HTML >Node基础篇(文件操作)_html/css_WEB-ITnose
Node内核提供了很多与文件操作相关的模块,每个模块都提供了一些最基本的操作API,在NPM中也有社区提供的功能包
fs:
基础的文件操作 API
path:
提供和路径相关的操作 API
readline:
用于读取大文本文件,一行一行读
fs-extra(第三方):
https://www.npmjs.com/package/fs-extra
在文件操作的过程中,都必须使用物理路径(绝对路径),path模块提供了一系列与路径相关的 API
console.log('join用于拼接多个路径部分,并转化为正常格式'); const temp = path.join(__dirname, '..', 'lyrics', './友谊之光.lrc'); console.log(temp);console.log('获取路径中的文件名'); console.log(path.basename(temp));console.log('获取路径中的文件名并排除扩展名'); console.log(path.basename(temp, '.lrc'));console.log('====================================');console.log('获取不同操作系统的路径分隔符'); console.log(process.platform + '的分隔符为 ' + path.delimiter);console.log('一般用于分割环境变量'); console.log(process.env.PATH.split(path.delimiter));console.log('====================================');console.log('获取一个路径中的目录部分'); console.log(path.dirname(temp));console.log('====================================');console.log('获取一个路径中最后的扩展名'); console.log(path.extname(temp));console.log('====================================');console.log('将一个路径解析成一个对象的形式'); const pathObject = path.parse(temp); console.log(pathObject);console.log('====================================');console.log('将一个路径对象再转换为一个字符串的形式'); // pathObject.name = '我终于失去了你';pathObject.base = '我终于失去了你.lrc'; console.log(pathObject);console.log(path.format(pathObject));console.log('====================================');console.log('获取一个路径是不是绝对路径'); console.log(path.isAbsolute(temp)); console.log(path.isAbsolute('../lyrics/爱的代价.lrc'));console.log('====================================');console.log('将一个路径转换为当前系统默认的标准格式,并解析其中的./和../'); console.log(path.normalize('c:/develop/demo\\hello/../world/./a.txt'));console.log('====================================');console.log('获取第二个路径相对第一个路径的相对路径'); console.log(path.relative(__dirname, temp));console.log('====================================');console.log('以类似命令行cd命令的方式拼接路径'); console.log(path.resolve(temp, 'c:/', './develop', '../application'));console.log('====================================');console.log('获取不同平台中路径的分隔符(默认)'); console.log(path.sep);console.log('====================================');console.log('允许在任意平台下以WIN32的方法调用PATH对象'); // console.log(path.win32);console.log(path === path.win32);console.log('====================================');console.log('允许在任意平台下以POSIX的方法调用PATH对象'); console.log(path === path.posix);
源码地址: https://github.com/nodejs/node/blob/master/lib/path.js
Node中文件读取的方式主要有:
fs.readFile('c:\\demo\1.txt', 'utf8', (err, data) => { if (err) throw err; console.log(data);});
try { const data = fs.readFileSync('c:\\demo\1.txt', 'utf8'); console.log(data);} catch(e) { // 文件不存在,或者权限错误 throw e;}
const stream = fs.createReadStream('c:\\demo\1.txt'); let data = '' stream.on('data', (trunk) => { data += trunk;});stream.on('end', () => { console.log(data);});
由于Windows平台下默认文件编码是GBK,在Node中不支持,可以通过 iconv-lite解决
const readline = require('readline'); const fs = require('fs');const rl = readline.createInterface({ input: fs.createReadStream('sample.txt')});rl.on('line', (line) => { console.log('Line from file:', line);});
Node中文件写入的方式主要有:
fs.writeFile('c:\\demo\a.txt', new Date(), (error) => { console.log(error);});
try { fs.writeFileSync('c:\\demo\a.txt', new Date());} catch (error) { // 文件夹不存在,或者权限错误 console.log(error);}
var streamWriter = fs.createWriteStream('c:\\demo\a.txt'); setInterval(() => { streamWriter.write(`${new Date}\n`, (error) => { console.log(error); });}, 1000);
// 相比较之前文件流的方式,这种方式不会占用文件资源,append完成就会释放setInterval(() => { fs.appendFile('c:\\demo\a.txt',`${new Date}\n`, (error) => { console.log(error); });}, 1000);
setInterval(() => { fs.appendFileSync('c:\\demo\a.txt',`${new Date}\n`);}, 1000);
与命令行相同,重命名操作也可以实现文件移动
相关链接:
实现思路:
const fs = require('fs'); const path = require('path'); var marked = require('marked'); var bs = require('browser-sync').create();var target = path.join(__dirname, process.argv[2] || './README.md'); var filename = path.basename(target, path.extname(target)) + '.html'; var targetHtml = path.join(path.dirname(target), filename);bs.init({ server: path.dirname(target), index: filename, notify: false});bs.reload(filename);var template = `<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> <style>{{{styles}}}</style></head> <body> <article class="markdown"> {{{body}}} </article></body> </html> `;fs.readFile(path.join(__dirname, './markdown.css'), 'utf8', (error, css) => { if (error) throw error; template = template.replace('{{{styles}}}', css); var handler = (current, previous) => { fs.readFile(target, 'utf8', (error, content) => { var html = template.replace('{{{body}}}', marked(content)); fs.writeFile(targetHtml, html, (error) => { if (!error) { console.log(`updated@${new Date()}`); bs.reload(filename); } }); }); }; handler(); fs.watchFile(target, { interval: 100 }, handler);});