-
Notifications
You must be signed in to change notification settings - Fork 0
/
async.js
120 lines (120 loc) · 3.82 KB
/
async.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
var fs = require('fs');
//Asynchronous
fs.readFile('input.txt',function(err, data){
if(err){
return console.error(err);
}
console.log("Asynchronous read: ", data.toString());
});
//Synchronous read
var data = fs.readFileSync('input.txt');
console.log("Synchronous read: "+ data.toString());
console.log("Program ended");
//fs.open(path, flags[, mode], callback)
//Asynchronous - Opening file
console.log("Going to open file");
fs.open('input.txt', 'r+', function(err, fd){
if(err){
return console.error(err);
}
console.log("File opened susscesslly");
});
//Get file info
//fs.stat(path, callback)
console.log("Going to get file info!");
fs.stat('input.txt', function(err, stats){
if(err){
return console.error(err);
}
console.log(stats);
console.log("Got file info successlly");
//check file type
console.log("is File ?"+ stats.isFile());
console.log("isDerectory ?"+ stats.isDirectory());
})
console.log("program dened");
//write File
//fs.writeFile(filename, data[, options], callback)
console.log("Going to write into extsting file");
fs.writeFile('write.txt','Going to write into extsting file', function(err){
if(err){
return console.error(err);
}
console.log("Data written successfully!");
fs.readFile('write.txt',function(err, data){
if(err){
console.error(err);
}
console.log("Asynchronous read:"+ data.toString());
});
})
//read a file
//fs.read(fd, buffer, offset, length, position, callback)
var buffer= new Buffer(1024);
console.log("Going to open an exitsting file.");
fs.open('write.txt', 'r+', function(err, fd){
if(err){
console.error(err);
}
console.log("File opened successfully!");
console.log("Going to read the file");
fs.read(fd,buffer, 0, buffer.length, 0, function(err, bytes){
if(err){
console.error(err);
}
console.log(bytes+ "byte read");
//print only read bytes to avoid junk.
if(bytes>0){
console.log(buffer.slice(0, bytes).toString());
}
});
});
//fs.close(fd, callback)
console.log("Going to open an exitsting file");
fs.open('write.txt', 'r+', function(err, fd){
if(err){return console.error(err);}
console.log("File opened successfully.");
console.log("Going to read a file");
fs.read(fd, buffer, 0, buffer.length, 0, function(err, bytes){
if(err){return console.error(err);}
//print only read bytes to avoid junk
if(bytes>0){
console.log(buffer.slice(0, bytes).toString());
}
//close to open file
fs.close(fd, function(err){
if(err){return console.error(err);}
console.log("File closed successfully.");
});
});
});
//Truncate a File
//fs.ftruncate(fd, len, callback
console.log("Going to open an exitsting file");
fs.open('write.tx','r+', function(err, fd){
if(err){return console.error(err);}
console.log("File opened successfully.");
console.log("Going to read the same file");
fs.ftruncate(fd, 10, function(err){
if(err){return console.error(err);}
fs.read(fd, buffer, 0 ,buffer.length, 0, function(err, bytes){
if(err){return console.error(err);}
//print only read bytes to avoid junk
if(bytes>0){
console.log(buffer.slice(0, bytes).toString());
}
//closed file
fs.close(fd, function(err){
if(err){return console.error(err);}
console.log("File closed successfully");
})
});
});
});
//fs.unlink(path, callback)
//Delete file
console.log("Going to delete an exitsting file");
fs.unlink('write.txt', function(err){
if(err){return console.error(err);}
console.log("File deleted sucessfully");
});