-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfileSystem.js
56 lines (35 loc) · 1.34 KB
/
fileSystem.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
var http = require('http');
var fs = require('fs');
// read file
http.createServer(function(req, res) {
fs.readFile('fileName.txt', function(err, data) {
res.writeHead(200);
res.write(data);
res.end();
});
}).listen(8080);
// append specific content at the end of file. If file is not created then file will be created.
fs.appendFile('fileName.txt', 'Add some text in file.\n', function(err){
if(err) throw err;
console.log('SAVED');
});
// The fs.open() method takes a "flag" as the second argument, if the flag is "w" for "writing", the specified file is opened for writing. If the file does not exist, an empty file is created.
fs.open('fileName.txt', 'w', function(err, file){
if(err) throw err;
console.log('FILE OPENED');
});
// The fs.writeFile() method REPLACES the specified file and content if it exists. If the file does not exist, a new file, containing the specified content, will be created.
fs.writeFile('fileName.txt', 'Add some text in file.\n', function(err){
if(err) throw err;
console.log('SAVED');
});
// The fs.unlink() method DELETE the specified file.
fs.unlink('fileName.txt', function(err){
if(err) throw err;
console.log('FILE DELETED');
});
// The fs.rename() method RENAME the specified file.
fs.rename('oldFileName.txt', 'newFileName.txt', function(err){
if(err) throw err;
console.log('FILE RENAMED');
})