
node.js是一个基于chrome v8引擎的javascript运行环境。【相关教程推荐:nodejs视频教程、编程教学】

区分版本号
lts为长期稳定版,推荐安装lts版本的node.js. current为新特性尝鲜版,对热衷于尝试新特性的同学来说,推荐安装current版本的node.js。
聊聊怎么用node写入读取文件内容-尊龙凯时
使用readfile读取文件内容
读取失败是一个error对象
成功的话就是undefined
// 1.导入fs模块,操作文件
const fs = require('fs');
// 2.调用readfile() 方法 来读取文件
// 第一个参数是被读取文件的路径
// 第二个参数是编码格式
// 第三个参数是回调函数,拿到读取成功(datastr)或者是失败的结果 (err)
fs.readfile('./file/01.text', 'utf8', function (err, datastr) {
console.log(err);// 打印失败的结果
console.log("---------------------");
console.log(datastr);// 打印成功的结果
})
判断文件是否读取成功
const fs = require('fs');
fs.readfile('./file/01.txt', 'utf8', function (err, datastr) {
if (err) {
return console.log('读取失败!' err.message);
}
console.log('读取成功!' datastr);
})成功

失败

const fs = require('fs');
// 三个参数
// 参数1表示文件存放路径
// 参数2表示要写入文件的内容
// 参数3回调函数
fs.writefile('./file/02.text', 'aic大山鱼', function (err) {
// 写入成功后err的值就是null,且在该文件夹下生成一个02文件
if (err) {
return console.log('文件写入失败!' err.message);
}
console.log('文件写入成功!');
})
思维梳理
要求:把一个文件的内容整理起来,放到另一个文件名字和分数用冒号分隔开
1.导入需要的fs文件系统模块
2.使用fs.readfile0方法,读取素材目录下的report-card.txt文件
3.判断文件是否读取失败
4.文件读取成功后,处理成绩数据
5.将处理完成的成绩数据,调用fs.writefile0 方法,写入到新文件report-card(1).txt中
// 导入fs模块
const fs = require('fs');
// 调用resdfile()方法 读取文件
fs.readfile('./file/report-card.txt', 'utf8', function (err, datastr) {
tostring(datastr);
// 判断是否读取成功
if (err) {
return console.log('读取失败!' err.message);
}
// 把获取到的成绩用逗号分隔开保存
const arrold = datastr.split(',');
// 循环分割后的每一个数组,进行字符串的替换操作
const arrnew = [];
// item代表要遍历那个数组里的每一项
arrold.foreach(item => {
// 把=替换成:
arrnew.push(item.replace('=', ':'))
});
// 把新数组的每一项进行合并得到新的字符串
const newstr = arrnew.join('\n');
// 使用writefile()方法,把处理完毕的数据写入到新文件中
fs.writefile('./file/report-card(1).txt', newstr, function (err) {
if (err) {
return console.log('写入失败!' err.message);
}
console.log('写入成功!');
})
})在使用fs模块操作文件时,如果提供的操作路径是以/或./开头的相对路径时,很容易出现路径动态拼接错误的问题。
原因:代码在运行的时候,会以执行node命令时所处的目录,动态拼接出**作文件的完整路径。
// __dirname 表示当前文件所处的目录
const fs = require('fs');
// 使用方法
fs.readfile(__dirname '/file/01.txt', 'utf8', function (err, datastr) {
if (err) {
return console.log('读取失败!' err.messages);
}
console.log('读取成功!' datastr);
})path模块是node.js官方提供的、用来处理路径的模块。它提供了一系列的方法和属性, 用来满足用户对路径的处理需求。
●path.join()方法,用来将多个路径片段拼接成一个完整的路径字符串
●path.basename()方法,用来从路径字符串中,将文件名解析出来
const path = require('path');
// ../会抵消一层路径
const pathstr = path.join('/a', '/v', '../', '/d', 'c');
console.log(pathstr);const path = require('path');
const fs = require('fs');
fs.readfile(path.join(__dirname, '/file/01.txt'), 'utf8', function (err, datastr
) {
if (err) {
return console.log(err.message);
}
console.log(datastr);
})path.basename使用
const path = require('path');
const fpath = '/a/d/c/index.html'
const fullname = path.basename(fpath);
console.log(fullname);
// 移除后缀名
const namewithoutext = path.basename(fpath, '.html');
console.log(namewithoutext);获取路径中扩展名文件
path.extname()方法
const path = require('paht');
const fpath = '/a/s/d/f/index.html'// 路径字符串
const fext = path.extname('fpath');
console.log(fext);// 输出.html我是aic山鱼,感谢您的支持
原 创 不 易 ?还希望支持一下
点赞?:您的赞赏是我前进的动力!
收藏?:您的支持我是创作的源泉!
评论?:您的建议是我改进的良药!
山鱼?社区:山鱼社区??
更多node相关知识,请访问:nodejs 教程!
以上就是聊聊怎么用node写入读取文件内容的详细内容,更多请关注其它相关文章!
舊人不必等