File System In Nodejs

·

3 min read

create the file and add text inside it

When we use the file system first we import the file system from the inbuilt fs modules.

const sunny = require("fs");

fs is an inbuilt module in the file system.

If you want to create a file and add some text inside the file then use writeFileSync() and writeFile() function.

const sunny = require("fs");
sunny.writeFileSync("./raushan.txt" , "Hello I am Sunny");

// Hello I am Sunny

writeFileSync() inside the parameter first argument takes the filename with extension and another argument can take the value that is inside the file. we can also create this file.

const sunny = require("fs");
sunny.writeFileSync("./raushan.html" , "<h1>Hello I am Sunny</h1>");

If you again run writeFileSync() changes the value, then it will override the value.

const sunny = require("fs");
sunny.writeFileSync("./raushan.txt" , "Hello I am Aditya");

// Hello I am Aditya

writeFile() we can also use this function in this function the first argument takes the path means the name of the file with an extension and the second argument takes the text inside this file and it also takes one more argument which is the callback function it will provide an error if an error occur.

const sunny = require("fs");
sunny.writeFile("./raushan.txt" , "Hello I am Tannu" , (error)=>{});

Read file

If any file is already present, if you want to read the content which is written inside that file then use this function readFileSync() or readFile() .

const sunny = require("fs");
const store = sunny.readFileSync("./Contact.txt","utf-8");
console.log(store);

readFileSync() This function takes two arguments in the first argument we provide the file name with a path and another argument stands for in which format you will be displaying the file, the file can be video format, binary format, txt format any other format can also occur that why we use utf-8 means you display the file in this format utf-8 .

readFileSync() it will return the values.

readFile() it will not return any value we can use this function in this format

const sunny = require("fs");
sunny.readFile("./Contact.txt","utf-8" , (error , result)=>{
if(error){
console.log(error);
}else
{
console.log(result);
}
});

readFile() it will take three arguments in its parameter. The first argument contains the path(filename with extension) Second argument contains utf-8 and the last argument contains a callback function inside the callback function it will take two arguments first one is an error and another is the result if any error occurs then an error will display and if any error does not occur then the result will display.

readFile() it will not return any value, it will always accept a callback value.

appendFileSync()

appendFileSync(), if you want to add some text(value) in the existing file then we use this function.

const sunny = require("fs");
  sunny.appendFileSync("./test.txt","Adding some value init");

After running the above code it will add the value that is given by you in the second argument. It will not override the values means any value that exists in that file then it will not be deleted.

cpSync()

cpSync this function is used to copy one file into another file.

const sunny = require("fs");
sunny.cpySync("./sunny.txt","./new.txt");

cpSync all the content of sunny.txt file is copied into new.txt file.

unLinkSync()

unLinkSync() This function is used to delete the file.

const sunny = require("fs");
sunny.unLinkSync("./new.txt");

unLinkSync() inside the parameter we provide the file name(path) which you want to delete.

mkdirSync()

mkdirSync() this function is use to create a directory(folder).we provide the name of the directory inside the parameter.

const sunny = require("fs");
sunny.mkdirSync("newSunny");

If you want to create a folder inside another folder then use this approach to create a folder inside the another folder.

const sunny = require("fs");
sunny.mkdirSync("newSunny/a/b" , {recursive: true});