Node JS copying files

/**
 * Created by Administrator on 2019/11/6.
 * Fingertips beating sun and yet the world ---- a handsome boy! ! ! .
 */
var fs=require("fs");
/**
 * 1: normal read and write
 */
fs.writeFileSync("D:/app/data/1.json",fs.readFileSync("D:/app/data/other/1.json"));
fs.writeFileSync("D:/app/data/temp/gn_image_0.jpg",fs.readFileSync("D:/app/data/other/gn_image_0.jpg"));
fs.writeFileSync("D:/app/data/temp/gn_image_1.jpg",fs.readFileSync("D:/app/data/other/gn_image_1.jpg"));

/**
 * Method 2: duct flow mode read and write
 * Pipe flow
 * / 
// Create a stream read 
var   readStream fs.createReadStream = ( "D: /app/data/other/1.json"); // copied files 
// create a stream of write 
var   writeStream = fs.createWriteStream ( "D: /app/data/1.json"); // copy the file to the target location and 
// read the written content stream to the output stream via conduit stream 
readStream.pipe (writeStream);

The above second approach is more suitable for reading large files

Guess you like

Origin www.cnblogs.com/luzhanshi/p/11804794.html