-
Notifications
You must be signed in to change notification settings - Fork 0
/
files.js
53 lines (38 loc) · 1004 Bytes
/
files.js
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
43
44
45
46
47
48
49
50
51
52
53
const fs = require('fs');
// reading files
fs.readFile('./docs/blog1.txt', (err, data) => {
if(err){
console.log(err);
}
console.log(data.toString());
})
console.log('last line'); //it will be printed before hello fifi is done,because that was asyncronous code and it doesnt block other code from executing
//writting files
fs.writeFile('./docs/blog2.txt', 'hello , now',() => {
console.log('file was written');
});
//directories
if(!fs.existsSync('./assets')){
fs.mkdir('./assets', (err) => {
if(err){
console.log(err);
}
console.log('folder created');
})
} else{
fs.rmdir('./assets', (err) => {
if(err){
console.log(err)
}
console.log('folder removed')
})
}
//deleting files
if(fs.existsSync('./docs/deleteme.txt')){
fs.unlink('./docs/deleteme.txt' , (err) => {
if(err){
console.log(err)
}
console.log('file deleted');
})
}