写文件遇见需要换行的情况就在需要换行的内容前面加换行符号就阔以了,简单
- \r\n 【windows】; \n【linux】 ; \r【mac】
- 下面是一个复制文件的方法,每次追加时间戳的时候希望他换行 >
appendFile()的时候内容前面加‘\r\n’
const fs = require('fs')
/**
* @description 复制文件
* @param {Object} originFilePath 复制源文件路径
* @param {Object} targetFilePath 目标文件路径
*/
function copyFile(originFilePath, targetFilePath) {
fs.readFile(originFilePath, (err, res) => {
if (err) {
return console.log(err)
}
fs.writeFile(targetFilePath, res, function(err, res) {
if (err) {
return console.log(err)
}
console.log('copy over')
fs.appendFile(targetFilePath, `\r\n${new Date().toLocaleString()}`, (err, res) => {
if (err) {
return console.log('append txt failed')
}
console.log('append file success')
})
})
})
}
copyFile('D:/aaa.txt', 'D:\\learnTS\\node_example\\poem102.txt')