// Common api of fs module // Read file, write file, append file, copy file, delete file // read file // fs.readFile(path[, options], callback) // fs.readFileSync(path[, options]) const fs = require('fs' ) // asynchronously read fs.readFile(' ./test.json', (error, data) => { if (error) return var data = data.toString() console.log(data) }) // Synchronous read try { let data = fs.readFileSync('./test.json','utf-8') console.log(data) } catch (error) { console.log(error) } // 文件写入 // fs.writeFile(file, data[, options], callback) // fs.writeFileSync(file, data[, options]) // write asynchronously fs.writeFile('./data/datalist.json','{"data":"test data"}', error => { if (error) { console.log(error) } }) // Synchronous write try { fs.writeFileSync( './data/datalist.json','{"data":"Test data 2"}' ) } catch (error) { console.log(error) } // 文件追加 // fs.appendFile(path, data[, options], callback) // fs.appendFileSync(path, data[, options], callback) // Append asynchronously fs.appendFile('./data/datalist.json', 'append data', err=> { if (err) console.log(err) }) // // Synchronous append try { fs.appendFileSync( './data/datalist.json','Sync append' ) } catch (error) { console.log(error); } // 文件拷贝 // fs.copyFile(src, dest[, flags], callback) // fs.copyFileSync(src, dest[, flags], callback) // 异步拷贝 fs.copyFile('./test.json','test_copy_async.json',err=>{ if(err) console.log(err); }) // // Synchronous copy try { fs.copyFileSync('./test.json','test_copy_sync.json') } catch (error) { } // File delete // fs.unlink(path, callback) // fs.unlinkSync(path, callback) // Delete asynchronously fs.unlink('./test_copy_async.json',err=> { if (err)console.log(err) }) // // Synchronous delete try { fs.unlinkSync('./test_copy_sync.json') } catch (error) { console.log(error) } // summary: // Synchronization is to add "Sync" after the original api, the parameters are similar to (path[, options], callback) // File reading: readFile // File writing: writeFile // File appending: appendFile // File copying: copyFile // File delete: unlink
folder related api
// Common api for folder operations const fs = require('fs' ) // Create a folder // fs.mkdir(path[, options], callback) // fs.mkdirSync(path[, options]) fs.mkdir( './static/js',{ recursive: true },err => { // recursive: true Creates the /static/css directory whether or not the /static directory exists. if (err)console.log(err); }) // Read the folder fs.readdir('./static',{withFileTypes: false },(err,data)=> { if (err){ console.log(err) return } console.log(data); // {withFileTypes:false}, returns an array of strings // [ 'aaa.text', 'css', 'images', 'js' ] // {withFileTypes:true}, returns an array of dirent objects // dirent.isFile() determines whether it is a regular file // dirent.isDirectory() determines whether it is a file directory // [ // Dirent { name: 'aaa. text', [Symbol(type)]: 1 }, // Dirent { name: 'css', [Symbol(type)]: 2 }, // Dirent { name: 'images', [Symbol(type)]: 2 }, // Dirent { name: 'js', [Symbol(type)]: 2 } // ] }) // delete file // fs.rmdir(path, callback) // fs.rmdirSync(path, callback) // ps: add options{recursive:true}, report error fs.rmdir('./static/images',err => { if (err) console.log(err) })
Reprinted in: https://www.cnblogs.com/superjsman/p/11606974.html