In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Today, the editor will share with you the relevant knowledge points about the contents of the Node file system, which are detailed and logically clear. I believe most people still know too much about this knowledge, so share this article for your reference. I hope you can get something after reading this article.
First, Node file system 1, FS (FileSystem): to achieve the IO operation of files. Fs is a module of Node and needs to be imported
Var fs = require ('fs')
2. Synchronous and asynchronous
(1) synchronization: when it comes to file IO, the program must wait until the end of the file IO operation before performing subsequent operations.
(2) Asynchronous: when it comes to file IO, the program does not wait for the end of the file IO operation, but continues to perform subsequent operations. When the file IO operation is over, the system will inform the program of the result of processing the file IO.
3. Callback function
"callback" means "call back"; "callback function" means that it is not called immediately after the function is defined, but is called when an event is triggered. Have the characteristic of asynchronism
4. Common functions of fs module (1) read files
A, readFile (): for reading data asynchronously. -non-blocking reading
Fs.readFile (fileName, function (err, buffer) {/ / asynchronously read file if (err) throw err;// code for processing files process (buffer);})
"filename": file name (including path)
"function (err,buffer)": callback function. 'err' stores the information that failed to read the file;' buffer' stores the contents of the file.
B, readFileSync (): used to read the file synchronously and return a string. -blocking mode
Var text = fs.readFileSync (fileName, 'utf8')
ReadFileSync (filename,'utf8') / / read the file synchronously. The return value of the function is a string (file content).
The second parameter: used to specify the character set. If not specified, the function returns Buffer;. If specified, the string is returned.
/ / create input.txt Happy New year / / create read,js//1. Import fs module const fs = require ('fs'); / / 2. Asynchronous read fs.readFile ('input.txt',function (err,buf) {if (err) {return console.error (err)} console.log ("Asynchronous read File:", buf.toString ()}) console.log (' 2022-1-2') / / 2022-1-2 Universe / Asynchronous read File: mountain and Sea can be flat / / Happy New year / / 1. Import fs module const fs = require ('fs'); / / 3. Synchronously read let str = fs.readFileSync ('input.txt','utf8'); console.log ("synchronously read files:", str); console.log (' 2022-1-2') / / synchronously read files: mountain and sea can be flat / / Happy New year / / 2022-1-2 (2) Open files fs.open (path, flags [, mode], callback)
Open (path, flags [, mode], callback)
'path': file name (including path)
'The way flags': opens the file
'mode': sets the file mode, but only if the file has been created. It defaults to 0666, read and write
'callback': this is a callback function with two parameters (err, fd)
-- 'ringing: open the file read-only, and report an exception if the file does not exist
-- 'wicked: open a file for writing and create a file if it is a file
-- 'asides: appends content to the file, and creates the file if the file
-- 'opening a file synchronously with rs':
/ / 1. Import fs module const fs = require ('fs'); fs.open (' input.txt','r+',function (err,fd) {if (err) {return console.error (err)} console.log ("fd:", fd); console.log ('file opened successfully!');}) / / fd: 3pm / file opened successfully! (3) get file information
Is an object that contains specific information about a file or directory (folder). From this object, you can determine whether you are currently dealing with an object or a directory.
Fs.stat (path, callback)
'path': file name, including the path string.
'The callback': callback function gets two parameters (err, stats)
/ / 1. Import fs module const fs = require ('fs'); fs.stat (' text',function (err,stats) {if (err) {return console.error (err)} console.log ("File status:", stats) / / check console.log ("isFile? -", stats.isFile ()) / / determine whether the object is a file console.log ("isDirectory?----", stats.isDirectory ()) / / determine whether the object is a directory}) / / File status: Stats {/ / dev: 3959906401, mode: 16822, nlink: 1, uid: 0, gid: 0 / / rdev: 0jtempact / blksize: 4096 ino / ino: 27584547718321690 / size: 0precinct / blocks: 0meme / atimeMs: 1645523918307.2014 / mtimeMs: 1645523918307.2014 / ctimeMs: 1645523918307.2014 / birthtimeMs: 1645523918307.2014 atime: 2022-02-2014 / / mtime: 2022-02-22T09 birthtime: 2022-02-22T09:58:38.307Z//} / / isFile?-false// isDirectory?---- true (4) writes to the file fs.writeFile (filename, data [, options], callback)
A, writeFile-write files asynchronously
'filename': file name, including path string
'The data': string or buffer will be written to the file
'options': an object that specifies the encoding format. The default encoding is UTF8.
The 'callback': callback function takes a parameter err that returns an error if any write error occurs.
Const fs = require ('fs') / / 1. Write content fs.writeFile ('input.txt',' Cloud and Sea', function (err) {if (err) {return console.error (err)} console.log ('write file successful!') to input.txt;})
B, writeFileSync (fileName, data, 'utf8'); / / write synchronously
Fs.writeFileSync (fileName, str, 'utf8'); (5) read files (read in binary) fs.read (fd, buffer, offset, length, position, callback)
'fd': is the descriptor of the file and is returned by the open function
'The buffer': buffer. Stores the contents read from the binary file
'offset': offset. Where to write to the buffer
'Bytes read by length':
'position': means to read from somewhere in the file. Start from the current location by default
'callback': callback function
Const fs = require ('fs'); / / read binary var buf = new Buffer (1024); fs.open (' input.txt','r+',function (err,fd) {if (err) {return console.error (err)} console.log ('file opened successfully!') Fs.read (fd,buf,0,buf.length,0,function (err,bytes) {if (err) {return console.error (err);} if (bytes > 0) {let str = buf.slice (0 last bytes). ToString (); console.log ("read is:", str) }}) console.log ('- End----');}) / / opened the file successfully! / /-End----// read the content as follows: weeds on the ancient plains grow in chaos, and every year spring comes to lush autumn to wither and yellow. (6) close the file close (fd, callback) const fs = require ('fs'); / / read the binary file var buf = new Buffer (1024) Fs.open ('input.txt','r+',function (err,fd) {if (err) {return console.error (err)} console.log (' file opened successfully!') Fs.read (fd,buf,0,buf.length,0,function (err,bytes) {if (err) {return console.error (err);} if (bytes > 0) {let str = buf.slice (0 last bytes). ToString (); console.log ("read is:", str) }}) fs.close (fd,function (err) {if (err) {return console.error (err)} console.log ('File closed successfully!)}) console.log ('-End----') }) / / Open the file successfully! / /-End----// reads: weeds on Guyuan grow at random, and every spring and autumn come withered and yellow / / the file closes successfully! (7) create a directory (folder) mkdir (path [, mode], callback)
'The directory name that path': wants to create, including the path
'permissions for mode': directory. Default is readable and writable.
Callback': callback function
Const fs = require ('fs'); fs.mkdir ('. / yunxi',function (err) {if (err) {return console.error (err)} console.log ('create directory successfully!')) (8) Delete directory rmdir (path, callback) const fs = require ('fs') Fs.rmdir ('. / text',function (err) {if (err) {return console.error (err)} console.log ('deleted successfully!')}) (9) read directory readdir (path, callback)
'path': directory name
The 'callback': callback function, which takes two arguments (err,files),' files' is a list containing the names of files or folders in the current directory
Onst fs = require ('fs'); fs.readdir ('. / yunxi',function (err,files) {if (err) {return console.error (err)} files.forEach (function (file) {console.log (file)}) / / demo.css// exam.ts// zhou (10) file copy
CopyFile (source file, target file, callback function)
Const fs = require ('fs'); fs.copyFile ('. / input.txt','./target.txt',function (err) {if (err) {return console.error (err)} console.log ('file copied successfully!')) / / target file copied the contents of the input file (11) append the content appendFile (filename,content,callback) to the file
'The content appended by content':
Const fs = require ('fs'); let content = "\ nWhen the wildfire burns forever, the spring breeze still thrives"; fs.appendFile ('. / input.txt',content,function (err) {if (err) {return console.error (err)} console.log ('additional content success!') / / input document is: weeds on the ancient plains grow at random, and every spring comes luxuriant and autumn turns yellow. Although the wildfire burns forever, the spring breeze still flourishes. (12) Delete the file unlink (path, callback)
'Filename of path': with path
Allback': callback function with no arguments
Const fs = require ('fs'); fs.unlink ('. / target.txt',function (err) {if (err) {return console.error (err)} console.log ('file deleted successfully!')}) (13) File rename rename (oldPath, newPath, callback)
'The original file name of oldPath':
'New newPath': filename
'callback': callback function
Const fs = require ('fs'); fs.rename ('. / input.txt','./yunxi.txt',function (err) {if (err) {return console.error (err)} console.log ('renamed successfully!')) / / renaming the input file to yunxi is all the content of the article "what are the contents of Node's file system?" Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to 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.