nodejs: file io

This commit is contained in:
Ciro Santilli 六四事件 法轮功
2019-10-08 00:00:01 +00:00
parent a9856a47a8
commit 50281d1db5
4 changed files with 29 additions and 5 deletions

View File

@@ -1,5 +1,7 @@
#!/usr/bin/env node
// https://cirosantilli.com/linux-kernel-module-cheat#node-js
for (const arg of process.argv) {
console.log(arg);
}

View File

@@ -0,0 +1,20 @@
#!/usr/bin/env node
// https://cirosantilli.com/linux-kernel-module-cheat#node-js
const assert = require('assert');
const fs = require('fs');
const path = require('path');
const file_path = path.basename(__filename) + '.tmp';
const data = 'asdf\nqwer\n';
const encoding = 'utf8';
// Write to file.
//
// Error handling through exceptions:
// https://stackoverflow.com/questions/15543235/checking-if-writefilesync-successfully-wrote-the-file
fs.writeFileSync(file_path, data, encoding);
// Read from file.
assert.strictEqual(fs.readFileSync(file_path, encoding), data);