You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: en/file-system/filesystem.md
+5-6Lines changed: 5 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,20 +1,20 @@
1
1
---
2
2
chapter: 24
3
3
---
4
-
# Filesystem
4
+
# File system
5
5
Filesystem operations in JavaScript are used to interact with the file system of the host environment, which can be a web browser (client-side JavaScript) or a server (Node.js). JavaScript provides various APIs and methods for reading from and writing to the file system. These operations are essential for tasks like saving data, reading configuration files, and processing user-uploaded files. Below is an overview of filesystem operations in JavaScript:
6
6
#### Asynchronous and Non-Blocking I/O:
7
7
8
8
In Node.js, I/O operations can be performed asynchronously, meaning that they don't block the execution of the program. Instead, they are placed in a queue, and the program continues executing other tasks. When the I/O operation is completed, a callback function is called to handle the result. This is particularly useful for I/O operations that may take a significant amount of time.
9
9
10
-
#####Read:
10
+
#### Read:
11
11
In this example, you are using the fs.readFile function to read data from the 'test.txt' file asynchronously. It takes a callback function that gets executed when the read operation is finished. The console.log("This gets printed at First") line is executed immediately after initiating the read operation, and it doesn't wait for the reading to complete.
12
12
13
13
```javascript
14
14
constfs=require('fs');
15
15
//async
16
16
//non blokcing i/0 thats why runs at last as ti takes moer time
17
-
fs.readFile('test.txt','utf8',(err,data)=>{
17
+
fs.readFile('test.txt','utf8',(err,data)=>{
18
18
console.log(err,data)
19
19
})
20
20
console.log("This gets printed at First")
@@ -24,8 +24,7 @@ console.log("This gets printed at First")
24
24
Here, you use fs.writeFile to write data to the 'test.txt' file asynchronously. The callback function is executed when the write operation is finished. It prints "This runs after writing in a file: written to file" after the write operation is complete.
25
25
26
26
```javascript
27
-
fs.writeFile("test.txt","mahima is good girl",()=>{
28
-
27
+
fs.writeFile("test.txt","mahima is good girl", () => {
29
28
console.log("This runs after writting in a file: written to file")
30
29
})
31
30
```
@@ -49,7 +48,7 @@ fs.writeFileSync is used for synchronous file writing. It blocks the program's e
49
48
50
49
51
50
```javascript
52
-
fs.writeFileSync("test.txt","mahima is good girl",()=>{
51
+
fs.writeFileSync("test.txt","mahima is good girl",()=>{
53
52
console.log("This is sync: intentionally process is blocked ")
0 commit comments