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--
In this issue, the editor will bring you about the operation of the directory in the Node.JavaScript file system. The article is rich in content and analyzes and describes for you from a professional point of view. I hope you can get something after reading this article.
When it comes to the operation of the directory, it needs to involve traversing the directory, in fact, the directory is that we can regard it as a tree. Tree is a kind of nonlinear data structure, which is used to store hierarchical data and ordered lists, in which there is a special kind of tree: binary tree.
There is a special binary tree called binary search tree (BST). Other features are that relatively small values are stored in the left node and larger values are saved in the right node, because this feature makes the search very efficient.
There are three ways to traverse a binary search tree: middle order, first order, and later order.
Middle order: according to the key value on the node, all the nodes in the tree have been accessed in ascending order, first the left subtree, then the root node, and finally the right subtree.
Intermediate order
First order: access the root node first, and then access the left and right subtrees in the same way
Preface
Second order: visit the leaf node first, from the left subtree to the right subtree, and then to the root node
Post-order
There are also two search methods: depth first search and breadth first search.
Depth-first search starts from the beginning of a path to the last vertex, then backwards, continues to trace the next path until it reaches the last vertex, and so on, until there is no path.
Depth first search
The breadth-first search starts from the first vertex, first checks the layer closest to the first vertex, and then gradually moves down to the layer farthest from the starting vertex.
Breadth first search
Create a directory synchronously
_ fs.accessSync_ is the synchronous method of fs.access used to check whether the file exists and whether it has read and write permissions to the file. When the operation is successful, the return value is the same as that of the asynchronous method, but an exception is thrown when the operation fails.
_ fs.mkdirSync_ is the synchronous creation directory
Needless to say, let's go straight to the code.
Let fs = require ("fs"); let path = require ("path") function mkdirSync (paths) {let arr = paths.split ("/"); for (let iIndemarr.length) return cb (); let currentPath = arr.slice (0m index1) .join ("/") Fs.access (currentPath, (err) = > {if (err) {fs.mkdir (currentPath, () = > next (index+1))} else {/ / do not create next (index+1)}} if present)} next (0)} mkdir ("a/b/c/d/e") (err) = > {console.log ("creation complete") })
Create a directory
Deep deletion of directories (synchronization)
The fs.stat () method is used to query file information, and can be used to query file size, creation time, permissions, and other related information. Fs.stat () is an asynchronous method, and a synchronous method fs.statSync (path) returns an object
The idea is: delete your son and then delete yourself on one branch, and then delete your son and then delete yourself on another branch.
Function removeSync (dir) {let statObj = fs.statSync (dir) if (statObj.isDirectory ()) {let dirs = fs.readdirSync (dir) / / returns an array / / console.log (dirs); for (let I = 0 * ipath.join (current,d)); / / current son's folder path arr = [. Arr,...dirs]}}
Result: ['a\ b\ c\ d']
Function wideSync (dir) {let arr = [dir]; let index = 0; current project read by let current; / / while (current = arr [index++]) {let statObj = fs.statSync (current); if (statObj.isDirectory ()) {let dirs = fs.readdirSync (current); dirs = dirs.map (d = > path.join (current,d)) / / the folder path of the current son arr = [... arr,...dirs]}} / / flashback to delete for (let I = arr.length-1;i > = 0poliimure -) {let current = arr [I] let statObj = fs.statSync (current) If (statObj.isDirectory ()) {fs.rmdirSync (current)} else {fs.unlinkSync (current)} wideSync ("a")
Deeply delete directory (serial asynchronous)
Function rmdirSeries (dir,callback) {fs.stat (dir, (err,statObj) = > {if (statObj.isDirectory ()) {/ / read file contents fs.readdir (dir, (err,dirs) = > {dirs = dirs.map (d = > path.join (dir)) D) function next (index) {if (index = = dirs.length) return fs.rmdir (dir,callback) / / delete the first one in the array first and then delete the second rmdirSeries (dirs [index]) () = > next (index+1)} next (0)})} else {fs.unlink (dir,callback)}})} rmdirSeries ("a", () = > {console.log ("deleted successfully") })
Deep directory deletion (parallel asynchronous)
Function removeDirParalle (dir,callback) {fs.stat (dir, (err,statObj) = > {if (statObj.isDirectory ()) {/ / read file contents fs.readdir (dir, (err,dirs) = > {if (dirs.length = = 0) {return fs.rmdir (dir) Callback)} dirs = dirs.map (d = > {let current = path.join (dir,d)) / / after everyone deletes, call done removeDirParalle (current,done); return current}) / / concurrent delete let index = 0 Function done () {if (+ + index = = dirs.length) {fs.rmdir (dir,callback)})} else {fs.unlink (dir,callback)}})} removeDirParalle ("a" () = > {console.log ("deleted successfully") }) the above is the operation of the directory in the Node.JavaScript file system shared by the editor. If you happen to have similar doubts, please refer to the above analysis to understand. If you want to know more about it, you are 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.