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

Share some Nodejs common file fs module API (summary)

青灯夜游
Release: 2021-08-27 09:11:22
forward
2277 people have browsed it

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;
    });
})
Copy after login

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)
})
Copy after login

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

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

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;
    });
})
Copy after login

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(&#39;message.txt&#39;, data, (err) => {
  if (err) throw err;
});
Copy after login

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;
    });
})
Copy after login

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()
})
Copy after login

Rename

Renaming includes renaming files and directories

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

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!

Related labels:
source:juejin.cn
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