In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Editor to share with you how to delete nodejs files, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
Methods: 1, use "var fs=require ('fs');" introduce fs module; 2, use "fs.unlink (path,callback: (err) = > void)" or "fs.rm (path,callback: (err) = > void)" to delete the file.
The operating environment of this tutorial: windows7 system, nodejs 14.15.4, DELL G3 computer.
In nodejs, you can use the built-in method of the File system Module (fs) to delete files / directories.
The fs module 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
...
Delete files (directories)
First introduce the fs module: var fs=require ('fs')
Then delete the file (directory) using the following method
Delete file: fs.unlink (path, callback: (err) = > void)
Delete the directory: fs.rmdir (path,callback: (err) = > void)
Both directories and files can be deleted: fs.rm (path,callback: (err) = > void)
Example: create and delete directories (files), read and write file streams
/ * 1. Fs.stat detects whether a file or directory exists (whether a directory file exists) 2. Fs.mkdir creates a directory (determines whether it exists before creation) 3. Fs.writeFile writes to a file (create a file if the file does not exist But cannot create directory) 4. Fs.appendFile write append file 5.fs.readFile read file 6.fs.readdir read directory 7.fs.rename rename 8. Fs.rmdir delete directory 9. Fs.unlink delete file * / var fs=require ('fs') / / fs is the core module of node.js. Without downloading and installing it, you can directly introduce / / 1. Fs.stat detects whether the file or directory fs.statSync () obtains the stats object synchronously and receives it through the return value. Fs.stat ('html',function (error,stats) {if (error) {console.log (error); return false;} console.log (' file:'+ stats.isFile ()); console.log ('directory:' + stats.isDirectory ());}) / / 2. Fs.mkdir create directory fs.mkdir ('css',function (error) {if (error) {console.log (error)) Return false;} console.log ('directory created successfully') / / 3. Fs.writeFile writes to a file (the previous content will be overwritten) (create if the file does not exist) the utf8 parameter can omit fs.writeFile ('123.txt' nodejs overwrite', 'utf8',function (error) {if (error) {console.log (error); return false) } console.log ('write successful');}) / / 4. Fs.appendFile append file fs.appendFile ('123.txt') this is the content written\ nThe error (error) {if (error) {console.log (error); return false;} console.log ('write successful') }) / / 5.fs.readFile reads the file fs.readFile ('123.txt reading instruction function (error,data) {if (error) {console.log (error); return false;} / / console.log (data); / / data is the read hexadecimal data. You can also add the encoding format "utf8" to the parameters to solve the hexadecimal problem; console.log (data.toString ()); / / read the information of all lines}) / / 6.fs.readdir reads the first level of content under the directory and gets all the files and folders under the directory. Fs.readdir ('html',function (error,data) {if (error) {console.log (error); return false;} console.log (data); / / data is an array type that contains the name of the folder and file (only the first-level directory content). Get all the directories under a folder}) / / 7.fs.rename rename 1. Rename 2. Cut file (move) fs.rename ('html/index.html','html/news.html',function (error) {if (error) {console.log (error); return false;} console.log (' name changed successfully') }) / / 8. Fs.rmdir delete directory rmdir this method can only delete directories, not delete files fs.rmdir ('abc directory', function (error) {if (error) {console.log (error); return false;} directory ('deleted directory successfully') }) / 9. Fs.unlink deletes the file fs.unlink ('index.txt',function (error) {if (error) {console.log (error); return false;} console.log (' deleted file successfully');}) / / 10. Fs.createReadStream reads data from the file stream. If the file is large, it is recommended to read it by stream. File comparison will be read many times. Var fileReadStream = fs.createReadStream ('data1.json'); var str =''; fileReadStream.on ('data', (data) = > {console.log ("received" + data.length); / / when the file is large, it will be read many times and execute the callback function str + = data;} many times) fileReadStream.on (' end', () = > {console.log ("- end -"); console.log (str)) }) fileReadStream.on ('error', (error) = > {console.log (error)}) / / 11. Fs.createWriteStream writes file stream var fs = require ("fs"); var data = "I got the data from the database and I want to save it"; var writerStream = fs.createWriteStream (' output.txt'); writerStream.write (data, 'UTF8'); writerStream.end () / / Mark the end of the write stream at the end of the file and release the resource writerStream.on ('finish', function () {console.log ("write complete.") ;}); writerStream.on ('error', function (error) {console.log (error.stack);}); console.log ("Program completed"); / / 12. Pipe pipeline flow var fs = require ("fs"); var readerStream = fs.createReadStream (' input.txt'); var writerStream = fs.createWriteStream ('output.txt'); readerStream.pipe (writerStream); console.log ("Program executed") / / 13. Fs.access determines whether directories and files exist (read and write permissions) var fs = require ('fs'); fs.access (' package.json', (err) = > {console.log (err? 'directory / file does not exist': 'file exists and can be read and written');}); above is all the content of the article "how to delete a file by nodejs". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.