0% found this document useful (0 votes)
5 views4 pages

3rd Program

The document outlines an experiment on file operations using Node.js, including creating, reading, appending, and deleting files. It provides code examples for each operation, such as using fs.writeFile() to create or overwrite a file, fs.readFile() to read its content, fs.appendFile() to add data, and fs.unlink() to delete the file. Each section includes instructions and expected outputs when the code is executed.

Uploaded by

chandrapuraja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

3rd Program

The document outlines an experiment on file operations using Node.js, including creating, reading, appending, and deleting files. It provides code examples for each operation, such as using fs.writeFile() to create or overwrite a file, fs.readFile() to read its content, fs.appendFile() to add data, and fs.unlink() to delete the file. Each section includes instructions and expected outputs when the code is executed.

Uploaded by

chandrapuraja
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Experiment 1: File Operations in Node.

js

Objective: To perform file operations such as read, write, append, and delete using Node.js.

Create Directry with name File _operations on Visual Studio Code

Function: fs.writeFile()

 This function writes data to a file.


 If the file does not exist, it creates a new file.
 If the file already exists, it overwrites the content.

create file name Write_name.js

const fs = require('fs');

// File path

const filePath = 'example.txt';

// Write content to the file

fs.writeFile(filePath, 'Hello, this is a test file.', (err) => {

if (err) {

console.error('Error writing file:', err);

} else {

console.log('File written successfully.');

});

When you run the file will get the following output and one file example.txt will be created in the
File_operations directry
Function: fs.readFile()

 Reads the content of a file.


 Returns the data as a buffer or string (if encoding is specified).

New file with name Read_file.js

const fs = require('fs');

// File path

const filePath = 'example.txt';

// Read the content of the file

fs.readFile(filePath, 'utf8', (err, data) => {

if (err) {

console.error('Error reading file:', err);

} else {

console.log('File contents:', data);

});

When you run the file will get the following output
Function: fs.appendFile()

 Appends new data to an existing file.


 If the file does not exist, it creates a new file and writes the data.

New file with name Append_file.js

const fs = require('fs');

// File path

const filePath = 'example.txt';

// Append content to the file

fs.appendFile(filePath, '\nAdditional content appended.', (err) => {

if (err) {

console.error('Error appending to file:', err);

} else {

console.log('Content appended successfully.');

});

When you run the file will see the folloing output

Note: to see the content appended run Read_file.js again


Function: fs.unlink()

 Deletes a file from the filesystem.

New file with name Delete_file.js

const fs = require('fs');

// File path

const filePath = 'example.txt';

// Delete the file

fs.unlink(filePath, (err) => {

if (err) {

console.error('Error deleting file:', err);

} else {

console.log('File deleted successfully.');

});

When you run the file will see the following output

You might also like