Home > Web Front-end > JS Tutorial > Detailed introduction to Fs module in NodeJs (code example)

Detailed introduction to Fs module in NodeJs (code example)

不言
Release: 2019-01-11 11:27:43
forward
3630 people have browsed it

This article brings you a detailed introduction (code example) about the Fs module in NodeJs. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

Fs module

  1. fs.stat detects whether it is a file or directory

//目录
fs.stat('html', function(err, stats) {
    if (err) {
        console.log(err);
        return false;
    }
    console.log('文件:' + stats.isFile());
    console.log('目录:' + stats.isDirectory());
})
//文件
fs.stat('index.txt', function(err, stats) {
    if (err) {
        console.log(err);

        return false;
    }
    console.log('文件:' + stats.isFile());
    console.log('目录:' + stats.isDirectory());
})
Copy after login

2.fs.mkdir Create directory

//接收参数:
//path          将创建的目录路径
//mode          目录权限(读写权限),默认0777
//callback      回调,传递异常参数err
fs.mkdir('css', function(err) {
    if (err) {
        console.log(err);
        return false;
    }
    console.log('创建目录成功');
})
Copy after login

3.fs.writeFile Create write file

//filename      (String)           文件名称
//data          (String | Buffer)  将要写入的内容,可以使字符串 或 buffer数据。
//options       (Object)           option数组对象,包含:
//· encoding    (string)           可选值,默认 ‘utf8′,当data使buffer时,该值应该为 ignored。
//· mode        (Number)           文件读写权限,默认值 438
//· flag        (String)           默认值 ‘w'
//callback {Function}              回调,传递一个异常参数err。

fs.writeFile('t.txt', '你好nodejs 覆盖', 'utf8', function(err) {
    if (err) {
        console.log(err);
        return false;
    }
    console.log('写入成功');
})
Copy after login

4.fs.appendFile Append file

fs.appendFile('t1.txt', '这是写入的内容', function(err) {
    if (err) {
        console.log(err);
        return false;
    }
    console.log('写入成功');
})
Copy after login

5.fs.readFile Read file

fs.readFile('t1.txt', function(err, data) {
    if (err) {
        console.log(err);
        return false;
    }
    //console.log(data);
    console.log(data.toString());
})
Copy after login

6.fs.readdir Read directory

fs.readdir('html', function(err, data) {
    if (err) {
        console.log(err);
        return false;
    }
    console.log(data);
})
Copy after login

7.fs.rename Rename

// 1. 改名 2. 剪切文件
fs.rename('html/index.html', 'html/news.html', function(err) {
    if (err) {
        console.log(err);
        return false;
    }
    console.log('修改名字成功');
})
Copy after login

8.fs.rmdir Delete Directory

fs.rmdir('t', function(err) {
    if (err) {
        console.log(err);
        return false;
    }
    console.log('删除目录成功');
})

// ENOENT: no such file or directory, rmdir      rmdir 这个方法只能删除目录
fs.rmdir('index.txt', function(err) {
    if (err) {
        console.log(err);
        return false;
    }
    console.log('删除目录成功');
})
Copy after login

9.fs.unlink Delete file

fs.unlink('index.txt', function(err) {
    if (err) {
        console.log(err);
        return false;
    }
    console.log('删除文件成功');
})
Copy after login

10.fs.createReadStream Read data from file stream

const fs = require('fs')
    //流的方式读取文件
let readStream = fs.createReadStream('input.txt');
let str = ''; /*保存数据*/
let count = 0; /*次数*/
readStream.on('data', function(chunk) {
        str += chunk;
        count++;
    })
    //读取完成
readStream.on('end', function(chunk) {
        console.log(count);
        console.log(str);
    })
    //读取失败
readStream.on('error', function(err) {
    console.log(err);
})
Copy after login

11.fs.createWriteStream Write file

let fs = require("fs");
let data = '我是从数据库获取的数据,我要保存起来11\n';
// 创建一个可以写入的流,写入到文件 output.txt 中
let writerStream = fs.createWriteStream('output.txt');
for (let i = 0; i < 100; i++) {
    writerStream.write(data, 'utf8');
}
//标记写入完成
writerStream.end();
writerStream.on('finish', function() {
        console.log('写入完成');
    })
    //失败
writerStream.on('error', function() {
    console.log('写入失败');
})
Copy after login

12. Pipe flow

let fs = require("fs");
// 创建一个可读流
let readerStream = fs.createReadStream('input.txt');
// 创建一个可写流
let writerStream = fs.createWriteStream('output.txt');
// 管道读写操作
// 读取 input.txt 文件内容,并将内容写入到 output.txt 文件中
readerStream.pipe(writerStream);
console.log("程序执行完毕");
Copy after login

The above is the detailed content of Detailed introduction to Fs module in NodeJs (code example). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template