node.js学习(03):fs模块

1、node中的fs模块是一个控制文件的模块
里面的功能是对文件进行操作,下面我们通过看nodejs源码的方式来学习

2、fs.stat 判断是路径还是文件
// 如果查找不到html路径或者目录,则返回err信息,并且不会
// 构建stats静态库,这时stats为undefined在这里插入代码片

nodejs源码

function stat(path: PathLike, options: StatOptions, callback: (err: NodeJS.ErrnoException | null,stats: Stats | BigIntStats) => void): void;
path是路径,options是设置一些参数,callback回调函数 err是错误信息,
states是静态数据对象(里面包含了一些方法)

对于err参数

interface ErrnoException extends Error {
        errno?: number;
        code?: string;
        path?: string;
        syscall?: string;
        stack?: string;
    }

对于stats参数

interface Stats extends StatsBase<number> {
    }
interface StatsBase<T> {
        isFile(): boolean;
        isDirectory(): boolean;
        isBlockDevice(): boolean;
        isCharacterDevice(): boolean;
        isSymbolicLink(): boolean;
        isFIFO(): boolean;
        isSocket(): boolean;
*/

实例:

fs.stat('./htmlx', (err, stats) => {
    if (err) {
        console.log(err);
        console.log(stats);
        return;
    }
    console.log();
    console.log(`是文件:${stats.isFile()}`);
    console.log(`是目录:${stats.isDirectory()}`);
});

3、fs.mkdir创建目录

亦可以创建文件
nodejs源码

function mkdir(path: PathLike, callback: NoParamCallback): void;

这里的回调函就是err

type NoParamCallback = (err: NodeJS.ErrnoException | null) => void;

实例:

fs.mkdir(`./css`, (err) => {
    if (err) {
        console.log(err);
        return;
    }
    console.log('创建成功');

});

4、writeFile 对文件进行写操作

//写文件 有则覆盖 无则创建
// 写入路径, 写入内容 报错信息 回调函数是一个err信息

  function writeFile(path: PathLike | number, data:any,callback: NoParamCallback): void;

在data和callback中间其实还有一个option对象设置参数

type WriteFileOptions = { encoding?: string | null; mode?: number
     | string; flag?: string; } | string | null;
    
    encoding  可选值 默认utf-8  当data是buffer时,该值为
    mode:文件读写权限  默认438
    flag:默认值 w

实例:

fs.writeFile('./html/index.html', 'JOJO', (err) => {
        if (err) {
            console.log(err);
            return;
        }
        console.log('写入成功了');
    })

5、fs.append 对文件进行追加操作

function appendFile(file: PathLike | number, data: any, callback: NoParamCallback): void;

和4一样,也有option option对照4

实例:

fs.appendFile('./html/base.css', 'body{color:red}', (err) => {
    if (err) {
        console.log(err);
        return;
    }
    console.log('写入成功了');
})

6、fs.readFile 以buffer来读文件

//readFile读文件 有option 但是没有mode 只以buffer读,所以我们读的时候要转

function readFile(path: PathLike | number, callback: (err: NodeJS.ErrnoException |
null, data: Buffer) => void): void;

实例:

fs.readFile('./html/index.html', (err, data) => {
    if (err) {
        console.log(err);
        return;
    }
    console.log(data);
    console.log(data.toString());//将buffer数据转为string
})

7、fs.readdir

//读取pathlike下的所有文件名或路径

 function readdir(path: PathLike, callback: (err: NodeJS.ErrnoException | null, files: string[]) => void): void;

8、fs.rename 重命名

function rename(oldPath: PathLike, newPath: PathLike, callback: NoParamCallback): void

9、fs.rmdir删除目录

//删除目录 目录下如果有文件,则无法删除

function rmdir(path: PathLike, callback: NoParamCallback): void;

10、fs.unlink删除文件

function unlink(path: PathLike, callback: NoParamCallback): void;

版权声明:本文为qq_43323305原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。