nodejs module fs-- file operations api

@ FS common module API 

//   read the file, write the file, the additional file, copy a file, delete a file 

//   read the file 
//   fs.readFile (path [, Options], the callback) 
//   fs.readFileSync (path [, Options]) 

const FS = the require ( 'FS' )
 //   asynchronous read 
fs.readFile ( './ test.json', (error, Data) => {
     IF (error) return 
    var Data = data.toString () 
    the console.log (Data) 
}) 
//   synchronous read 
the try { 
    the let Data = fs.readFileSync ( './ test.json', 'UTF-. 8' ) 
    the console.log (Data) 
} the catch (error) {
    the console.log (error) 
} 

//   file writing 
//   fs.writeFile (File, Data [, Options], the callback) 
//   fs.writeFileSync (File, Data [, Options]) 

//   asynchronous write 
fs.writeFile ( './data/datalist.json','{"data ":" test data "}', error => {
         IF (error) { 
            the console.log (error) 
        } 
    }) 
//   synchronous writes 
the try { 
    FS .writeFileSync ( './data/datalist.json','{"data ":" test data 2 "}' ) 
} the catch (error) { 
    the console.log (error) 
} 

//   file addition 
//  fs.appendFile (path, Data [, Options], the callback) 
//   fs.appendFileSync (path, Data [, Options], the callback) 

//   asynchronous added 
fs.appendFile ( './ data / datalist.json' , ' added data ', ERR => {
    IF (ERR) the console.log (ERR) 
}) 
// // synchronous adding 
the try { 
    fs.appendFileSync ( ' ./data/datalist.json ',' additional synchronization ' ) 
} the catch (error ) { 
    the console.log (error); 
} 

//   copy files 
//   fs.copyFile (the src, dest [, the flags], the callback) 
//   fs.copyFileSync (the src, dest [, the flags], the callback) 

//   asynchronous copy
fs.copyFile ( './ test.json', 'test_copy_async.json', ERR => {
     IF (ERR) the console.log (ERR); 
}) 
// // synchronous copying 
the try { 
    fs.copyFileSync ( './ test.json ',' test_copy_sync.json ' ) 
} the catch (error) { 
    
} 

//   file deletion 
//   fs.unlink (path, the callback) 
//   fs.unlinkSync (path, the callback) 

//   asynchronous remove 
fs.unlink ( './test_copy_async.json',err=> {
     IF (ERR) the console.log (ERR) 
}) 
// // delete sync 
the try { 
    fs.unlinkSync ( ' ./test_copy_sync.json ') 
} The catch (error) { 
    the console.log (error) 
} 

// Summary: 

// sync are added "Sync" at the back of the original api, similar parameters (path [, Options], the callback) 
// file reads: readFile 
/ / file write: writeFile 
// file append: appendFile 
// copy files: copyFile 
// file deletion: unlink

 

Guess you like

Origin www.cnblogs.com/superjsman/p/11606974.html