fs module in Nodejs

1. File write operation

writeFile directly opens the file in w mode by default, so if the file exists, the content written by this method will overwrite the old file content

grammar:

writeFile(file, data[, options], callback) asynchronous
writeFileSync(file, data) Synchronize

parameter:

file file name
data the data to write to the file
options The parameter is an object containing {encoding, mode, flag}. The default encoding is utf8, the mode is 0666, and the flag is 'w'
callback Callback function, the callback function only contains the error message parameter (err), and returns when writing fails.

code:

// 1、导入fs模块
const fs = require('fs')
// 2、异步写入文件
fs.writeFile('./xxx.txt', '三人行, 则必有我师焉', err => {
    // err写入失败: 错误对象   写入成功: null
    if(err) {
        console.log('写入失败')
        return
    }
    console.log('写入成功')
})

// 2、同步写入文件
fs.writeFileSync('./data.txt', 'text')

2. Additional writing

method one,

grammar:

appendFile(file, data, option, callback) Synchronize
appendFileSync(file, data) asynchronous

parameter:

       

file appended files
data Additional content
option The parameter is an object containing {encoding, mode, flag}. The default encoding is utf8, the mode is 0666, and the flag is 'w'
callback callback after operation

code:

const fs = require('fs')


// 1、异步追加
fs.appendFile('./xxx.txt', '你说嘴巴嘟嘟,还是狗吧狗狗', err => {
    // 判断
    if(err) {
        console.log('写入失败~~')
        return
    }
    console.log('写入成功~~')
})


// 同步追加
fs.appendFileSync('./xxx.txt', '你说嘴巴嘟嘟,还是狗吧狗狗')

 Method two,

code:

const fs = require('fs')

fs.writeFile('./xxx.txt', '你说嘴巴嘟嘟,还是狗吧狗狗', {flag: 'a'}, err => {
    // 判断
    if(err) {
        console.log('写入失败~~')
        return
    }
    console.log('写入成功~~')
})

3. File stream writing

code:

// 1、导入fs
const fs = require('fs')

// 2、创建写入流对象
const ws = fs.createWriteStream('./观书后感.txt')

// 3、write
ws.write('半亩方塘一鉴开\r\n')
ws.write('天光云影共徘徊\r\n')
ws.write('问渠那得清如许\r\n')
ws.write('为有源头活水来\r\n')

// 关闭通道
ws.close();

4. File reading

method illustrate
readFile asynchronous read
readFileSync synchronous read
createReadStream streaming read

code:

// 1. fs模块
const fs = require('fs')


// 2. 异步读取
fs.readFile('./观书后感.txt', (err, data) => {
    if(err) {
        console.log('读取失败~~')
        return
    }
    console.log('读取成功~~', data.toString())
})


// 3. 同步读取
let data = fs.readFileSync('./观书后感.txt');
console.log(data.toString())


// 4. 流式读取
const rs = fs.createReadStream('./data.txt')  // 创建读取流对象

rs.on('data', res => {  // 绑定data事件
    console.log(res)
})

rs.on('end', () => {
    console.log('读取完成')
})

 Effect:

5. Copying of files

code:

// 导入fs模块
const fs = require('fs')

// 方式一
// 1、读取文件
let data = fs.readFileSync('./data.txt')
// 2、写入文件
fs.writeFileSync('./data2.txt', data);


//方式二 流式操作(推荐使用)
// 1、创建读取流对象
const rs = fs.createReadStream('./data.txt')
// 2、创建写入流对象
const ws = fs.createWriteStream('./data3.txt')
// 3. 绑定data事件
rs.on('data', chunk => {
    ws.write(chunk)
})

6. File move and rename

grammar:

rename(oldPath, newPath, callback) Synchronize
renameSync(oldPath, newPath) asynchronous

parameter:

oldPath the current path of the file
newPath path to the new file
callback callback after operation

code:

const fs = require('fs')

// 修改名字
fs.rename('./data.txt', './new_data.txt', err => {
    if(err) {
        console.log('操作失败~~')
        return
    }
    console.log('操作成功')
})

// 移动
fs.rename('./data2.txt', './data/new_data.txt', err => {
    if(err) {
        console.log('操作失败~~')
        return
    }
    console.log('操作成功')
})

7. File deletion

grammar:

unlink(path, callback) asynchronous
unlinkSync Synchronize
rm(path, callback) Version 14.4

parameter:

path file path
callback callback after operation

code:

const fs = require('fs')

fs.unlink('data3.txt', err => {
    if(err) {
        console.log('删除失败')
        return
    }
    console.log('删除成功')
})

8. Folder operation

1. Create

grammar:

mkdir(path, options, callback) asynchronous
mkdirSync(path, options) Synchronize

parameter:

path file path
options option configuration
callback callback after operation

code:

const fs = require('fs')

// 1、创建单个文件夹 
fs.mkdir('./html', err => {
    if(err) {
        console.log('创建失败~~')
        return
    }
    console.log("创建成功~~")
})

// 2、创建递归文件夹
fs.mkdir('./a/b/c', {recursive: true}, err => {  // 需要添加{recursive: true} 参数
    if(err) {
        console.log('创建失败~~')
        return
    }
    console.log("创建成功~~")
})

2. Read

grammar:

readdir(path, callback) read folder

parameter:

path read folder
callback callback after operation

code:

const fs = require('fs')
fs.readdir('./data', (err, data) => {
    if(err) {
        console.log('读取失败')
        return
    }
    console.log(data);
})

3. Delete

grammar:

rmdir(path, option, callback) not recommended
rm(path, option, callback) Recommended Use

parameter:

path path
option Other parameters
callback callback after operation

.0

code:

const fs = require('fs')
// 删除
fs.rmdir('./html', err => {
    if(err) {
        console.log("删除失败")
        return
    }
    console.log("删除成功")
})

// 递归删除   删除a/b/c
fs.rmdir('./a', {recursive: true}, err => {
    if(err) {
        console.log("删除失败")
        return
    }
    console.log("删除成功")
})


// 推荐使用
fs.rm('./a', {recursive: true}, err => {
    if(err) {
        console.log("删除失败")
        return
    }
    console.log("删除成功")
})

 

Guess you like

Origin blog.csdn.net/qq_52421092/article/details/130090657