Home >Web Front-end >JS Tutorial >Share some Nodejs common file fs module API (summary)

Share some Nodejs common file fs module API (summary)

青灯夜游
青灯夜游forward
2021-08-12 09:56:022272browse

This article organizes and records some Nodejs file fs module API commonly used in work, so as not to forget it next time.

Share some Nodejs common file fs module API (summary)

As a web development engineer, it is inevitable to deal with Nodejs. The fs module is very useful and can perform some file-related operations, but I always forget it. , Forgot to remember. I plan to organize and record it again today to avoid forgetting it next time.

Synchronization and asynchronous

The file operations of the fs module generally support both synchronous and asynchronous APIs, and asynchronous also includes callback functions and promsie forms. Synchronization is usually followed by the words sync. [Recommended learning: "nodejs Tutorial"]

Open and close files

fs.open(path:string,callback: (err,fd)=>void) is used to open a file, obtain the file descriptor (file descriptor), and perform file operations based on the obtained file descriptor. fs.close(fd:number,callback:(err)=>void) Used to close the file

//打开文件
fs.open(path,(err,fd)=>{
    //针对拿到的fd 进行操作
    
    //关闭文件
    fs.close(fd, (err) => {
        if (err) throw err;
    });
})

Generally used to perform various operations on files. If you only want to read the contents of the file, it is recommended to use fs.readFile

Read the file (directory)

Read the file:fs.readFile(path:string,callback:(err,data)=>void)

fs.readFile(path,(err,data)=>{
    //string或者buffer
    console.log(data)
})

Read directory:fs.readdir(path:string,callback:(err , files:Array98c455a79ddfebb79781bff588e7b37e)=>void)

fs.readdir("./dir",(err,fileNames)=>{
    console.log(fileNames)
})

There is another way to read through the file descriptor:

fs.read(fd, buffer,offset,length,position,callback:(err,bytesLen,buffer)=>void)

//分配一块长度为10的缓存区
const buffer = Buffer.alloc(10);
//打开文件
fs.open(path,(err,fd)=>{
    //针对拿到的fd 进行操作:将fd对应的文件内容读取到buffer里
    //position为文件的起点
    //length为读取的长度
    //offset为缓存区起读的位置
    fs.read(fd,buffer,offset,length,position,(err,bytesLen,buffer)=>{
        //buffer为包含读到数据的原始buffer对象
        //bytesLen === length;// true
    })
    //关闭文件
    fs.close(fd, (err) => {
        if (err) throw err;
    });
})

Write the file

Write the data Enter the file, the data can be string or buffer:fs.writeFile(path,data,callback:(err)=>void)

fs.writeFile('message.txt', data, (err) => {
  if (err) throw err;
});

There is another way to write a file through the file descriptor fd:

fs.open(path,(err,fd)=>{
    //针对拿到的fd 进行操作:将buffer内容写如fd对应的文件里
    //position为文件的起点
    //length为待写的长度
    //offset为缓存区起写的位置
    fs.write(fd,buffer,offset,length,position,(err,bytesWrittenLen,buffer)=>{

    })
    //关闭文件
    fs.close(fd, (err) => {
        if (err) throw err;
    });
})

Delete file (directory)

Delete files: fs.unlink(path, callback:(err)=>void)

Delete directories: fs.rmdir(path, callback:(err)=&gt ;void)

Supports deleting directories and files at the same time: fs.rm(path,callback:(err)=>void)

View the status information of the directory (file)

fs.stat(path,(err,stat)=>{
    //stat包含了该目录或文件的大小、创建时间、更新时间,是目录还是文件等
    //stats.isDirectory()
    //stats.isFile()
})

Rename

Renaming includes renaming files and directories

//文件
fs.rename('oldFile.txt', 'newFile.txt', (err) => {
  if (err) throw err;
  console.log('Rename complete!');
});
//目录
fs.rename('oldFileDir', 'newFileDir', (err) => {
  if (err) throw err;
  console.log('Rename complete!');
});

Finally

Thank you for reading. If you have any questions, please leave a message for discussion. Thank you!

For more programming-related knowledge, please visit: Introduction to Programming! !

The above is the detailed content of Share some Nodejs common file fs module API (summary). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete