-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathfs-module.js
130 lines (108 loc) · 4.81 KB
/
fs-module.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
121
122
123
124
125
126
127
128
129
/**
* ---------------
* File System
* ---------------
*
* The fs module provides an API for interacting with the file system in a manner closely modeled around standard POSIX functions.
*
*
* --------------------------------
* Important method of fs module
* --------------------------------
*
* ---------------------------------- ----------------------------------------
* Method Description
* ---------------------------------- ----------------------------------------
* fs.readFile(fileName [,options], callback) Reads existing file.
* fs.writeFile(filename, data[, options], callback) Writes to the file. If file exists then overwrite the content otherwise creates new file.
* fs.open(path, flags[, mode], callback) Opens file for reading or writing.
* fs.rename(oldPath, newPath, callback) Renames an existing file.
* fs.chown(path, uid, gid, callback) Asynchronous chown.
* fs.stat(path, callback) Returns fs.stat object which includes important file statistics.
* fs.link(srcpath, dstpath, callback) Links file asynchronously.
* fs.symlink(destination, path[, type], callback) Symlink asynchronously.
* fs.rmdir(path, callback) Renames an existing directory.
* fs.mkdir(path[, mode], callback) Creates a new directory.
* fs.readdir(path, callback) Reads the content of the specified directory.
* fs.utimes(path, atime, mtime, callback) Changes the timestamp of the file.
* fs.exists(path, callback) Determines whether the specified file exists or not.
* fs.access(path[, mode], callback) Tests a user's permissions for the specified file.
* fs.appendFile(file, data[, options], callback) Appends new content to the existing file.
*
*
*
*
* --------- -------------------------------------------------
* Flag Description
* --------- -------------------------------------------------
* r Open file for reading. An exception occurs if the file does not exist.
* r+ Open file for reading and writing. An exception occurs if the file does not exist.
* rs Open file for reading in synchronous mode.
* rs+ Open file for reading and writing, telling the OS to open it synchronously. See notes for 'rs' about using this with caution.
* w Open file for writing. The file is created (if it does not exist) or truncated (if it exists).
* wx Like 'w' but fails if path exists.
* w+ Open file for reading and writing. The file is created (if it does not exist) or truncated (if it exists).
* wx+ Like 'w+' but fails if path exists.
* a Open file for appending. The file is created if it does not exist.
* ax Like 'a' but fails if path exists.
* a+ Open file for reading and appending. The file is created if it does not exist.
* ax+ Like 'a+' but fails if path exists.
*
*/
const fs = require('fs');
const dir = __dirname;
const path = dir + `\\assets\\temp.txt`;
// Example: Reading a directory
fs.readdir('./assets', (err, files) => {
if (err) {
throw err;
}
console.log(files);
});
console.log('Reading a Directory...');
// Example: Open and Read file
fs.open(path, 'r', function(err, fd) {
if (err) {
return console.error(err);
}
var buffr = new Buffer(1024);
fs.read(fd, buffr, 0, buffr.length, 0, function(err, bytes) {
if (err) throw err;
// Print only read bytes to avoid junk.
if (bytes > 0) {
console.log(buffr.slice(0, bytes).toString());
}
// Close the opened file.
fs.close(fd, function(err) {
if (err) throw err;
});
});
});
// Example: Reading File Synchronously
var data = fs.readFileSync(path, 'utf8');
console.log(data);
// Example: Renaming a file
fs.rename(path+'\\assets\\temp.txt', path+'\\assets\\dummy.txt', err => {
if (err) throw err;
console.log('Rename complete');
});
// Example: Creating & Writing File
fs.writeFile(dir + '\\assets\\test.txt', 'This is first paragraph!', function(err) {
if (err) console.log(err);
else console.log('Write operation complete.');
});
// Example: Append File Content
fs.appendFile(dir + '\\assets\\test.txt', '\nThis is second paragraph!', function(err) {
if (err) console.log(err);
else console.log('Append operation complete.');
});
// Example: File Status
fs.stat(path, (err, stats) => {
if (err) throw err;
console.log(`stats: ${JSON.stringify(stats)}`);
});
// Example: Deleting a file
fs.unlink(path, err => {
if (err) throw err;
console.log(`Successfully deleted file: ${path}`);
});