In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to read and write files in nodejs". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
In node, the method to read the file is "readFile ()", the syntax is "readFile (Path, (error,data) = > {})", the method to write the file is "writeFile ()", and the syntax is "writeFile (path,data, (err) = > {})".
This tutorial operating environment: 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 the computer.
Using the fs module, we can do the following:
Create files and directories
Modify files and directories
Delete files and directories
Read the contents of files and directories
...
Write to a file
To write a file from a Node.js application, use the writeFile method.
Syntax: fs.writeFile (path,data,callback: (err) = > void)
The writeFile method accepts at least the following parameters:
File name
Content
Callback
If the specified file already exists, it replaces the old content with what you 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 boyboys, err = > {if (err) throw err process.stdout.write ('created successfully!')})
The above code creates a new file called content.txt and adds the text All work and no play makes Jack a dull boy! As content. If there is any error, the callback function throws the error. Otherwise, it will output the file to the console and create it successfully.
There are other variations of writeFile, such as:
Fs.writeFileSync-write files synchronously
FsPromises.writeFile-write files using Promise-based API
Read from a file
Before reading the file, you need to create and store the path to the file. The path to the path module is convenient here.
Using the path method in the join module, you can create a file path as follows:
Const filePath = path.join (process.cwd (), 'content.txt')
The first parameter, process.cwd (), returns the current working directory. Now that you have the file path, you can read the contents of the file.
Write the following code in the file:
Fs.readFile (filePath, (error, data) = > {if (error) throw error process.stdout.write (data)})
The readFile method accepts at least two parameters:
Path to the file
Callback
If there is an error, it throws an error. Otherwise, it outputs the contents of the file in the terminal.
There are other variations of readFile, such as:
Fs.readFileSync-write files synchronously
FsPromises.readFile-write files using Promise-based API
That's all for "how nodejs reads and writes files". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.