How to quickly get started with VUE3.0: Enter the learning
The path module is used to process paths and files, and provides many methods.
One requirement ofis to concatenate the path and file name.
const basePath = '/user/why' const filename = 'abc.txt'
Then someone will use string splicing to splice.
const filePath = basePath + '/' + filename console.log(filePath);
Although there is no problem with such a result, considering different systems, the Windows system can use or \ or / as the path separator, while the Unix operating systems of Mac OS and Linux use / as the path separator. symbol.
To solve the above problem, we can use path.resolve to splice paths.
const path = require('path') const basePath = '/user/why' const filename = 'abc.txt' const filePath = path.resolve(basePath, filename) console.log(filePath);
const path = require('path') const filePath = '/User/haha/abc.txt' console.log(path.dirname(filePath)); console.log(path.basename(filePath)); console.log(path.extname(filePath));
If we want to splice multiple paths, but different operating systems may use different delimiters, we can use the path.join function.
const path = require('path') const basepath = '/User/haha' const filename = 'abc.txt' const filePath = path.join(basepath, filename) console.log(filePath);
If we want to splice a file and a folder, we can use path.resolve.
const basepath = 'User/haha' const filename = 'abc.txt'
Path.resolve and path.join can also be used to splice paths, so what is their difference?
const basepath = '../User/haha' const filename = './abc.txt' const othername = './haha.js' const filePath1 = path.join(basepath, filename, othername) console.log(filePath1); const filePath2 = path.resolve(basepath, filename, othername) console.log(filePath2);
We can see the difference.
Most APIs ofnodejs file system provide three operating methods:
Synchronous file operation: the code will be blocked and will not continue to execute
the asynchronous callback function. File operation: the code will not be blocked and a callback function needs to be passed in when the result is obtained. , the callback function executes
an asynchronous Promise operation file: the code will not be blocked. Calling method operations through fs.promises will return a Promise, which can be processed through then and catch.
method 1 synchronous operation: fs.statSync
const fs = require('fs') const filepath = './abc.txt' const info = fs.statSync(filepath) console.log('Code that needs to be executed later'); console.log(info);
Method 2 asynchronous operation
fs.stat(filepath, (err, info) => { if(err) { console.log(err); return } console.log(info); console.log(info.isFile()); // Determine whether it is a file console.log(info.isDirectory()); // Determine whether it is a folder}) console.log('Code that needs to be executed later');
Method three: Promise
fs.promises.stat(filepath).then(info => { console.log(info); }).catch(err => { console.log(err); }) console.log('Code to be executed subsequently');
node assigns a numeric file descriptor to all open files. All file system operations use these file descriptors to identify and track each specific file.
The fs.open() method is used to allocate a new file descriptor fd. Once allocated, the file descriptor can be used to read data from the file, write data to the file, or request information about the file.
const fs = require('fs') fs.open('./abc.txt', (err, fd) => { if(err) { console.log(err); return } // Get file information through file descriptors fs.fstat(fd, (err, info) => { console.log(info); }) })
fs.readFile(path[, options], callback): Read the file content
fs.writeFile(path[, options], callback): Write the content to the file
option parameters:
flag: written Mode
encoding: character encoding
file writing
fs.writeFile('./abc.txt', content, {flag: "a"}, err => { console.log(err); })
File reading
fs.readFile('./abc.txt', (err, data) => { console.log(data); })
If encoding is not filled in, the result Buffer (binary) will be returned.
fs.readFile('./abc.txt', {encoding: 'utf-8'}, (err, data) => { console.log(data); })
Use fs.mkdir() or fs.mkdirSync to create a new folder.
const fs = require('fs') //Create folder const dirname = './haha' if(!fs.existsSync(dirname)) { fs.mkdir(dirname, (err) => { console.log(err); }) }
fs.readdir
fs.readdir(dirname, (err, files) => { console.log(files); })
Get all the files in the folder. At this time, the directory is as shown in the figure below. You can use recursion.
const fs = require('fs') const path = require('path') const dirname = './haha' function getFiles(dirname) { fs.readdir(dirname, {withFileTypes: true}, (err, files) => { // console.log(files); for(let file of files) { // Determine whether it is a folder if(file.isDirectory()) { const filepath = path.resolve(dirname, file.name) getFiles(filepath) } else { console.log(file.name); } } }) } getFiles(dirname)
You can use fs.rename to rename the folder.
fs.rename('./haha', './xixi', err => { console.log(err); })