Slip 7 - A) Using Node.js create a web page to read two file names from user and append contents of first file into second file.

Solution:

var fs=require('fs');
 
fs.appendFile('f1.txt','f2.txt',
function(err){
if(err) throw err;
console.log('Updated');
});



f1.txt

"Welcome to BBA(CA)

f2.txt

BBA(CA) Department

Post a Comment

2 Comments

  1. file2 content not appended only name of file can be appenede

    ReplyDelete
  2. const readline = require('readline');
    const fs = require('fs');

    const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
    });

    // Prompt user for file names
    rl.question('Enter the name of the first file: ', (file1) => {
    rl.question('Enter the name of the second file: ', (file2) => {
    // Read content of file1
    fs.readFile(file1, 'utf8', (err, data) => {
    if (err) {
    console.error('Error reading file1:', err);
    rl.close();
    return;
    }

    // Append content of file1 to file2
    fs.appendFile(file2, data, (err) => {
    if (err) {
    console.error('Error appending to file2:', err);
    rl.close();
    return;
    }

    console.log(`Contents of ${file1} appended to ${file2} successfully!`);
    rl.close();
    });
    });
    });
    });

    ReplyDelete