-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathfile_read_write.js
60 lines (36 loc) · 1021 Bytes
/
file_read_write.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
'use strict';
//http://stackabuse.com/reading-and-writing-json-files-with-node-js/
const fs = require('fs');
let rawdata = fs.readFileSync('student.json');
let student = JSON.parse(rawdata);
console.log(student);
fs.readFile('student.json', (err, data) => {
if (err) throw err;
let student = JSON.parse(data);
console.log(student);
});
console.log('This is after the read call');
let student = {
name: 'Mike',
age: 23,
gender: 'Male',
department: 'English',
car: 'Honda'
};
let data = JSON.stringify(student);
fs.writeFileSync('student-2.json', data);
let jsonData = require('./student.json');
console.log(jsonData);
let student = {
name: 'Mike',
age: 23,
gender: 'Male',
department: 'English',
car: 'Honda'
};
let data = JSON.stringify(student, null, 2);
fs.writeFile('student-3.json', data, (err) => {
if (err) throw err;
console.log('Data written to file');
});
console.log('This is after the write call');