What is the method of reading and writing files in nodejs

青灯夜游
Release: 2021-12-31 13:59:45
Original
4481 people have browsed it

In node, the method for reading files is "readFile()", the syntax is "readFile(Path,(error,data)=>{})"; the method for writing files is "writeFile()" ", syntax "writeFile(path,data,(err)=>{})".

What is the method of reading and writing files in nodejs

The operating environment of this tutorial: windows7 system, nodejs version 12.19.0, DELL G3 computer.

In nodejs, the file system module (fs for short) allows us to access and interact with the file system on our computer.

Using the fs module, we can perform the following operations:

  • Create files and directories

  • Modify files and directories

  • Delete files and directories

  • Read the contents of files and directories

  • ...

Write File

To write a file from a Node.js application, use the writeFile method.

Syntax: fs.writeFile(path,data,callback:(err)=>void)

##writeFile method accepts at least the following parameters :

    Filename
  • Content
  • Callback
If the specified file already exists, it will replace the old content with yours The content provided as a parameter. If the specified file does not exist, a new file is created.

After importing the

fs and path modules, write the following code in the file:

fs.writeFile('content.txt', 'All work and no play makes Jack a dull boy!', err => {
  if (err) throw err

  process.stdout.write('创建成功!')
})
Copy after login

The above code will create a file named ## A new file of #content.txt

and added the text All work and no play makes Jack a dull boy! as content. If there are any errors, the callback function will throw that error. Otherwise, it will output to the console that the file was created successfully.

writeFile

There are other variations, such as:

    fs.writeFileSync
  • — Write files synchronously
  • fsPromises.writeFile
  • — Write a file using Promise-based API
  • Read from a file

Before reading a file, you need to create and store the file path.

path

The module's path is convenient here. Using the

path

method in the join module, you can create a file path like this: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:js;toolbar:false;">const filePath = path.join(process.cwd(), &amp;#39;content.txt&amp;#39;)</pre><div class="contentsignin">Copy after login</div></div>First parameter

process.cwd()

Returns the current working directory. Now that you have the file path, you can read the file's contents. Write the following code in the file:

fs.readFile(filePath, (error, data) => {
  if (error) throw error

  process.stdout.write(data)
})
Copy after login

readFile

The method accepts at least two parameters:

The path to the file
  • Callback
  • If there is an error, it will throw an error. Otherwise, it prints the file contents in the terminal.

readFile

There are other variations, such as:

    fs.readFileSync
  • — Write files synchronously
  • fsPromises.readFile
  • — Use Promise-based API to write files
  • For more node-related knowledge, please visit:
nodejs tutorial

! !

The above is the detailed content of What is the method of reading and writing files in nodejs. For more information, please follow other related articles on the PHP Chinese website!

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