-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfs_module.js
More file actions
42 lines (30 loc) · 2.61 KB
/
fs_module.js
File metadata and controls
42 lines (30 loc) · 2.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
Basically, file is a sequence of 0s and 1s stored in the memory. The data represented by these 0 and 1s entirely depend on the decoding procedure. Everything in our computer is a file.
E.g., bits in a text file does not represent the same as those in a image, therefore both of them need to be decoded differently.
1. Node can not access files directly, it talks to the OS with system-calls to request files (using libuv)
2. We can use either Promises API or Callback API or Synchronous API to perform any operation using fs_module.
*/
const fs = require("node:fs"); //Module include korlam
const path = require('node:path')
let text = fs.readFileSync("dele.txt","utf-8")
//Prothom argument ta je file ke read korbo tar path, second ta oi file tar encoding type
console.log(text); //readfilesync function ta dele.txt ke read kore text er moddhe store korlo
/*
==================================
Important: Relative paths will be resolved relative to the current working directory as determined by
calling process.cwd() i.e.the current working directory of the Node.js process.
==================================
*/
text= text.replace("Evening","Morning");
// fs.writeFileSync("../Foo.txt",text); // parent folder e file ta banate
// fs.writeFileSync("Foo.txt",text); //current folder e file ta banate
// fs.writeFileSync("Foo.txt","All I dream of your eyes"); //Evabe direct string diyeo lekha jay
// fs.writeFileSync("Foo.txt","All I want is your time",{flag:"a"}); //Evabe third argument e "a" flag dile jodi file ta already exist kore tahole setar ja data ache setar sathe ei input ta jure jabe
// Specifying relative path combined with __dirname
let file=fs.readFileSync(path.join(__dirname,'dele.txt')) // this ensures the relative paths are always resolved properly irrespective of the pwd of the node process.
/*
1. WriteFileSync function ta file create kore, jodi given location e oi nam e kono file present na thake. Thakle setar data overwrite kore notun data put korbe. Prothom argument ta jekhane file banabo tar path, second ta oi file er moddhe ki thakbe seta denote korbe
2. Jodi chai kono file e je data ache seta overwritten na hoye just por por jure jak... tokhn aro ekta argument pass korte hobe writeFile e, which is an object
3. Jodi onno kono directory te write korte chai, sei directory ta jeno age theke exist kore, nahole error debe.
4. It is synchronous in nature, i.e. it blocks the main thread while execution. Moreover, we can not handle errors in this approach and our whole application halts if an error is encountered.
*/