Skip to content

Commit 465af08

Browse files
committed
Adds examples for File System related utility functions;
Signed-off-by: Ashwin Hegde <ashwin.hegde3@gmail.com>
1 parent f9e13a6 commit 465af08

11 files changed

Lines changed: 285 additions & 0 deletions

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Hi,
2+
My name is Ashwin Hegde.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Hi,
2+
Welcome to the world of Node.js.

Kit/Session-5/FS/FixContent/file-a.txt

Whitespace-only changes.

Kit/Session-5/FS/directory-1.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/***
2+
* The goal of this file is to know about the file system functions;
3+
*
4+
*
5+
* How to run this example:
6+
* 1. node directory-1.js
7+
* 2. See the message get displayed on prompt.
8+
*/
9+
10+
var fs = require('fs'),
11+
dirName = 'Folder1';
12+
13+
/***
14+
* Create a directory.
15+
*/
16+
fs.mkdir(dirName, function(error) {
17+
if (error) throw error;
18+
console.log('Directory created successfully.');
19+
});
20+
21+
/***
22+
* Removed a directory.
23+
*/
24+
fs.rmdir(dirName, function(error) {
25+
if (error) throw error;
26+
console.log('Directory removed successfully.');
27+
});

Kit/Session-5/FS/directory-2.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/***
2+
* The goal of this file is to know about the file system functions;
3+
*
4+
*
5+
* How to run this example:
6+
* 1. node directory-2.js
7+
* 2. See the message get displayed on prompt.
8+
*/
9+
10+
var fs = require('fs'),
11+
dirName = 'FixContent';
12+
13+
/***
14+
* Read a directory.
15+
*/
16+
fs.readdir(dirName, function(error, files) {
17+
if (error) throw error;
18+
console.log('Following are the directory contents: ');
19+
console.log(files);
20+
});

Kit/Session-5/FS/fileSys-1.js

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/***
2+
* The goal of this file is to know about the file system functions;
3+
*
4+
*
5+
* How to run this example:
6+
* 1. node fileSys-1.js
7+
* 2. See the message get displayed on prompt.
8+
*/
9+
10+
var fs = require('fs'),
11+
path = require('path'),
12+
filename1 = path.normalize('FixContent/email1.txt'),
13+
filename2 = 'message.txt',
14+
filename3 = path.normalize('FixContent/file-a.txt'),
15+
filename4 = path.normalize('FixContent/file-b.txt');
16+
17+
/***
18+
* Read a file.
19+
*/
20+
fs.readFile(filename1, 'utf-8', function(error, data) {
21+
if (error) throw error;
22+
console.log('Following are the file content:');
23+
console.log(data);
24+
});
25+
26+
/***
27+
* Write content to a file. If file already exists, Node will remove it and write it.
28+
*/
29+
fs.writeFile(filename2, 'Hello World', function(error) {
30+
if (error) throw error;
31+
console.log('File is written successfully.');
32+
});
33+
34+
/***
35+
* Append content to a file. If file already exists, Node will open it and append the content.
36+
*/
37+
fs.appendFile(filename2, '\nWelcome to Node.js training sessions.', function(error) {
38+
if (error) throw error;
39+
console.log('Content appended successfully.');
40+
});
41+
42+
/***
43+
* Remove a file.
44+
*/
45+
fs.unlink(filename2, function(error) {
46+
if (error) throw error;
47+
console.log('%s removed successfully.', filename2);
48+
});
49+
50+
/***
51+
* Rename a file.
52+
*/
53+
fs.rename(filename3, filename4, function(error) {
54+
if (error) throw error;
55+
console.log('%s renamed successfully.');
56+
});

Kit/Session-5/FS/fileSys-2.js

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/***
2+
* The goal of this file is to know about the file system functions;
3+
*
4+
*
5+
* How to run this example:
6+
* 1. node fileSys-2.js
7+
* 2. See the message get displayed on prompt.
8+
*/
9+
10+
var fs = require('fs'),
11+
path = require('path'),
12+
filename1 = path.normalize('FixContent/email1.txt'),
13+
filename2 = path.normalize('FixContent/email15.txt');
14+
15+
/***
16+
* Check a file already exists.
17+
*/
18+
fs.exists(filename1, function(exists) {
19+
if (exists) {
20+
console.log('%s already exists.', filename1);
21+
} else {
22+
console.log('%s does not exists.', filename1);
23+
}
24+
});
25+
26+
27+
/***
28+
* Check a file already exists.
29+
*/
30+
fs.exists(filename2, function(exists) {
31+
if (exists) {
32+
console.log('%s already exists.', filename2);
33+
} else {
34+
console.log('%s does not exists.', filename2);
35+
}
36+
});

Kit/Session-5/FS/fileSys-3.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/***
2+
* The goal of this file is to know about the file system functions;
3+
*
4+
*
5+
* How to run this example:
6+
* 1. node fileSys-3.js
7+
* 2. See the message get displayed on prompt.
8+
*/
9+
10+
var fs = require('fs'),
11+
path = require('path'),
12+
filename1 = path.normalize('FixContent/email1.txt'),
13+
filename2 = 'message.txt';
14+
15+
/***
16+
* Open a file.
17+
*/
18+
19+
/***
20+
* Read a file.
21+
*/
22+
23+
/***
24+
* Write a file.
25+
*/
26+
27+
/***
28+
* Close a file.
29+
*/

Kit/Session-5/FS/fileSys-4.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/***
2+
* The goal of this file is to know about the file system functions;
3+
*
4+
*
5+
* How to run this example:
6+
* 1. node fileSys-4.js
7+
* 2. See the message get displayed on prompt.
8+
*/
9+
10+
var fs = require('fs'),
11+
path = require('path'),
12+
filename1 = path.normalize('FixContent/email2.txt');
13+
14+
/***
15+
* Keep a watch on file with watch.
16+
*/
17+
fs.watch(filename1, function(event, filename) {
18+
console.log('%s event got triggered.', event);
19+
if (filename) {
20+
console.log('%s is provided.', filename);
21+
} else {
22+
console.log('%s is not provided.', filename);
23+
}
24+
});
25+
26+
/***
27+
* Keep a watch on file with watchFile.
28+
*/
29+
fs.watchFile(filename1, function(curr, prev) {
30+
console.log('The current modified time is: ' + curr.mtime);
31+
console.log('The previous modified time was: ' + prev.mtime);
32+
});
33+
34+
/***
35+
* Unwatch a file.
36+
*/
37+
fs.unwatchFile(filename1);

Kit/Session-5/FS/fileSys-5.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/***
2+
* The goal of this file is to know about the file system functions;
3+
*
4+
*
5+
* How to run this example:
6+
* 1. node fileSys-5.js
7+
* 2. See the message get displayed on prompt.
8+
*/
9+
10+
var fs = require('fs'),
11+
path = require('path'),
12+
filename1 = path.normalize('FixContent/email2.txt');
13+
14+
/***
15+
* createReadStream a file.
16+
*/
17+
var data = fs.createReadStream(filename1, {flags:'r', encoding:'utf-8', start:2, end:8, autoClose:true});
18+
console.log(data);
19+
20+
/***
21+
* createWriteStream a file.
22+
*/
23+

0 commit comments

Comments
 (0)