Lab1: Node.js feature–copyFile

Copying file is the most common operation in real life.  In Node.js, there are two copy-file functions:

similar:copy file from source to destination, if the destination file exit, overwrite the destination file. When an error happens during writing to an opened file, the destination file may be removed by Node.js. Both functions have the ‘src’ and ‘dest’ parameters.

difference: fs.copyFileSync returns ‘undefined’ while fs.copyFile has a callback as a parameter. Also, the usage of them is different. CopyFileSync is often used at the more important scene in which the backup and the origin need to be synchronized in real-time.

example of fs.copyFile(src, dest[, flags], callback):

fs=require('fs');
fs.copyFile('origin.txt','backup.txt',(err)=>{
if (err) console.log('nothing happened. the err code is '+err.toString());
else console.log('the origin file has copied to the backup file');
});

source:

https://nodejs.org/api/fs.html#fs_fs_copyfile_src_dest_flags_callback

https://nodejs.org/api/fs.html#fs_fs_copyfilesync_src_dest_flags

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s