{})"; the method of writing files is "writeFile()", the syntax is "writeFile(path ,data,(err)=>{})”."/> {})"; the method of writing files is "writeFile()", the syntax is "writeFile(path ,data,(err)=>{})”.">

Home  >  Article  >  Web Front-end  >  What is the method of reading and writing files in nodejs

What is the method of reading and writing files in nodejs

青灯夜游
青灯夜游Original
2021-12-31 13:57:154464browse

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('创建成功!')
})

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: <pre class="brush:js;toolbar:false;">const filePath = path.join(process.cwd(), &amp;#39;content.txt&amp;#39;)</pre>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)
})

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!

Statement:
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