Nodejs module: fs

/ ** 
 * @description FS module common api 
 * / 

// FS all file operations are asynchronous IO, if you want to call in a synchronized manner, will add a plus in the original Sync synchronization on the basis of api 
// synchronized manner will in the last pass in a callback, asynchronous transfer mode will not callback 
// asynchronous flaw is unable to control abnormal, the catch can only use the try 

const FS = the require ( 'FS'); 
const path = the require ( 'path'); 

const = path.resolve fileReadPath (__ dirname, 'demoRead.txt'); 

// read the file 
// readFile readFileSync 
the try { 
    const = fs.readFileSync Data (fileReadPath, 'UTF8'); 
    // the console.log (Data) 
} the catch (ERR) { 
    console.error ( 'error reading from file:', err.message) 
    fs.writeFileSync (fileReadPath, '123,123,423'); 
} 


fs.readFile (fileReadPath, 'UTF8', function (ERR, Data) {
    if (err) return console.error ( 'Error reading from file:', err.message) 
    the console.log ( 'file contents:', Data) 
}) 

// read the folder, the folder corresponding to the current acquired all directory 
// the readdir readdirSync 
// returns a list of all files which path is path 
const = fs.readdirSync the readdir (path.resolve (__ dirname, '../fs')); 
the console.log (the readdir) 







const = path fileWritePath .resolve (__ dirname, 'demoWrite.html'); 

// write to file 
// asynchronous write, callback that transmission error 
// the writeFile writeFileSync 
fs.writeFile (fileWritePath, '<HTML> 123123 </ HTML>', function (ERR) { 
    IF (ERR) the throw ERR; 
    // the console.log ( 'write success') 
}) 

the try { 
    fs.writeFileSync (fileWritePath, '<HTML> 123123 </ HTML>');
    // console.log ( 'written successfully') 
} the catch (ERR) { 
    the console.log (ERR) 
} 





const = dirPath1 path.resolve (__ dirname, 'demoDir1'); 

// Create a directory 
// mkdir mkdirSync 
fs.mkdir (dirPath1,function(err) {
    if (err) {
        The console.log // (ERR) 
        return 
    }; 
    // the console.log ( 'create the directory success') 
}) 

const = dirPath2 path.resolve (__ dirname, 'demoDir2'); 

the try { 
    fs.mkdirSync (dirPath2); 
    FS. writeFileSync (dirPath2 + '/demoDir2File.txt', 'dqwdjwdojdojqwd'); 
    fs.mkdirSync (dirPath2 + '/ balabala'); 
    fs.writeFileSync(dirPath2 + '/balabala/balabalaFile.txt', 'dqwdjwdojdojqwd');
} catch(err) {
    // console.log(err);
} 





// check for 
// existsSync 
const = isExists fs.existsSync (path.resolve (__ dirname, 'demoRead.txt')); 
console.log ( 'isExists:', isExists) 

// the callback exists almost no, now with the Access 
fs.access (path.resolve (__ dirname, 'demoRead.txt'), function (ERR) { 
    IF (ERR) the console.log (ERR) 
    the console.log ( 'Have this document ') 
});




 
Analyzing // is a folder or a file, to get the file STAT, then calls methods 
// STAT statSync 
// the isDirectory () isFile () 
const = fs.statSync STAT (path.resolve (__ dirname, 'demoDir1')); 
IF (stat.isDirectory ()) { 
    the console.log ( 'the current folder is a') 
} the else IF (stat.isFile ()) { 
    the console.log ( 'a current file') 
} the else { 
    Console.log ( 'neither file nor the folder'); 
} 






// delete files 
// The unlink unlinkSync 
const = filePath path.resolve (__ dirname, 'demoRead.txt'); 
IF (fs.existsSync (filePath)) { 
    fs.unlinkSync (filePath); 
    the console.log ( 'successfully deleted files') 
} 

// delete folders 
// rmdir rmdirSync 
// Note that if the file there folder folder, the error will be deleted, the folder you want to delete all files must be, or not, can not have a folder 
// If there is, you must recursively delete 
IF (fs.existsSync (dirPath2)) { 
    the try { 
        fs.rmdirSync (dirPath2);
    The catch} (ERR) { 
        delDir (dirPath2) 
    } 
    the console.log ( 'successfully deleted folder') 
} 

// recursive delete files or folders 
function delDir (filePath) { 
    IF (fs.existsSync (filePath)) {  
        const = fs.statSync STAT (filePath);
        IF (STAT .isDirectory ()) { 
            // this is a folder, there is also need to determine whether a folder 
            for all files // needs to traverse the folder, and determines whether the folder or file, i.e., recursive call 
            const files = fs.readdirSync (filePath); 
            files.forEach (function (Item) { 
                delDir (path.resolve (filePath, Item)); 
            }); 
            fs.rmdirSync (filePath); 
        } the else IF (stat.isFile ()) { 
            // this is file, delete 
            fs.unlinkSync (filePath); 
        }
    The else {} 
        the console.log ( 'the file does not exist!') 
    } 
}

  

Guess you like

Origin www.cnblogs.com/yanchenyu/p/11300346.html