......
3. Operation on file structure
3.1 fs.readdir(url,(error,data)=>{})
Role: Read all files in the folder
Reference: Path to url folder
Two parameters: callback function, built-in two parameters, error information and resource handle
const fs = require('fs'); //Asynchronous read mode fs.readdir('./file',(error,data)=>{ if(error):console.log('Failed to read folder'); else{ console.log(data) } }); //Synchronized read with fewer callback functions and more return values let data = fs.readdirSync('./file'); console.log(data);
3.2 fs.stat(url,(error,data)=>{});
Role: Read information about files and folders
Reference: path of url target file and folder;
Two parameters: callback function, built-in two parameters, error information and resource handle
const fs = require('fs'); //Read files/folders asynchronously fs.stat('./file/aa',(error,data)=>{ //Verify that it is a folder error?console.log('read failure'):console.log(data.isFile()); }); //Synchronous reading of files/folders has fewer callback functions and more return values than asynchronous reading let result = fs.statSync('./file/aa'); console.log(result.isFile());//Boolean Value Read files synchronously/Information in Folder size Property is the byte length of the file read
3.3 fs.mkdir(url,{recursive:true},error=>{})
Role: (Iteration) Create folders
Reference: Path created by url
Two parameters: {recursive:true} Whether to iterate creation
Three meals: Callback function built-in parameter, error message
//Introducing js core module - file management module fs const fs = require('fs'); //Asynchronous Operation Create File fs.mkdir('./file/aa/bb/cc',{recursive:true},error=>{ error?console.log('Failed to create'):console.log('Created successfully'); }); //Synchronization Create File fs.mkdirSync('./file/dd/ee/hh',{recursive:true});
3.4 fs.rmdir(url,{recursive:true},error=>{})
Role: (Iteration) Delete folder
Reference: Path to url deletion
Two parameters: {recursive:true} Whether to iteratively delete
Three meals: Callback function built-in parameter, error message
//Introducing core module fs const fs = require('fs'); //Asynchronous operation to delete files fs.rmdir('./file/aa',{recursive:true},error=>{ error?console.log('Delete failed'):console.log('Delete succeeded'); }); //Synchronization Delete Files fs.rmdirSync('./file/dd',{recursive:true});
3.5 fs.unlink(url + filename, error=>{})
Role: Delete files
Reference: Path + File Name
Two parameters: Callback function built-in parameter, error message
const fs = require('fs'); //Asynchronous operation to delete files fs.unlink('./file/a.txt',error=>{ error?console.log('Delete failed'):console.log('Delete succeeded') }); //Synchronization Delete Files fs.unlinkSync('./file/b.txt');
3.6 fs. Rename (original url + original file name, new url + new file name, error=>{})
Effect:
1. Move and rename the file by modifying both the url and the file name;
2. Moving files only changes url, not rename;
3. Rename the file without moving the url, just rename it;
Reference: original url + original file name;
Second parameter: new url + new file name;
Three parameters: Callback function has a built-in parameter, which is the error information;
const fs = require('fs'); //Asynchronous operations move files and rename them fs.rename('./file/js.jpg','javaScript.jpg',error=>{ error?console.log('Move and rename failed'):console.log('Move and rename successfully'); }); //Synchronization moves and renames fs.renameSync('javaScript.jpg','./file/js.jpg');
3.7 Downloading a file reads the file and writes it to a new address file
fs.readFile('original url + file name', (error, resource handle) =>{
if(error){
console.log('Failed to read file');
}else{
//Write to file after successful reading
fs.writeFile('new url + file name', resource handle, {read method}, error=>{});
}; )
Role: Download the file, that is, read the file and write to the new address file
Reference: original url + original file name;
Two parameters: callback function, built-in two parameters, error information and resource handle date
Three parameters: new url + new file name;
Four parameters: resource handle data
Five parameters: reading method {flag:'w'}
Six parameters: Callback function has a built-in parameter, which is the error information;
Note: The suffix names must be the same when reading and downloading files.
const fs = require('fs'); //Read and write the same file asynchronously fs.readFile('./file/js.jpg',(error,data)=>{ if(error) console.log('read failure'); else { //Write to file after successful reading fs.writeFile('javaScript.jpg',data,{flag:'w'},error=>{ error?console.log('Write failed'):console.log('Download Successful'); }) } })
4. Global approach
4.1 __dirname
Role: Get the absolute path of the folder where the file is located
console.log(__dirname);
4.2 __filename
Role: Get the absolute path to the location of the file
console.log(__filename);
(Temporarily updated at 2021.07.24 18:16 Please correct any errors...)