How to use the openSync function from fs
Find comprehensive JavaScript fs.openSync code examples handpicked from public code repositorys.
fs.openSync is a synchronous method in Node.js that opens a file and returns its file descriptor.
126 127 128 129 130 131 132 133 134 135
~~~js const fs = require('fs') try { const fd = fs.openSync('/tmp/flydean.txt', 'r') } catch (err) { console.error(err) } ~~~
+ 5 other calls in file
GitHub: DataDog/dd-trace-js
26 27 28 29 30 31 32 33 34 35 36
} function maybePath (filepath) { if (!filepath) return try { fs.openSync(filepath, 'r') return filepath } catch (e) { return undefined }
How does fs.openSync work?
fs.openSync is a synchronous method in Node.js's fs module used to open a file with a specified flag and mode, returning a file descriptor that can be used for subsequent read or write operations.
GitHub: blockcollider/bcnode
66 67 68 69 70 71 72 73 74 75
exports.mkdir = co.promisify(fs.mkdir); exports.mkdirSync = fs.mkdirSync; exports.mkdtemp = co.promisify(fs.mkdtemp); exports.mkdtempSync = fs.mkdtempSync; exports.open = co.promisify(fs.open); exports.openSync = fs.openSync; exports.read = co.promisify(fs.read); exports.readSync = fs.readSync; exports.readdir = co.promisify(fs.readdir); exports.readdirSync = fs.readdirSync;
120 121 122 123 124 125 126 127 128 129
function readAsync(fd, chunkSize) { /* impl */ } function appendAsync(fd, buffer) { /* impl */ } function encrypt(buffer) { /* impl */} // 打开一个 4GB 的文件,每次只读取 64k var inFile = fs.openSync('4GBfile.txt', 'r+'); var outFile = fs.openSync('Encrypted.txt', 'w+'); readAsync(inFile, 2 << 15) .map(encrypt)
+ 3 other calls in file
Ai Example
1 2 3 4 5 6 7 8
const fs = require("fs"); try { const fileDescriptor = fs.openSync("example.txt", "w"); console.log(`File descriptor: ${fileDescriptor}`); } catch (err) { console.error(err); }
In this example, we use fs.openSync to open a file named "example.txt" in write mode. The function returns a file descriptor, which we then log to the console. If there was an error, it will be caught and logged to the console.
189 190 191 192 193 194 195 196 197 198
const stringifyStream = json.createStringifyStream({ body: data, }); // create empty file const fd = fs.openSync(filepath, 'w'); stringifyStream.on('data', (strChunk) => { fs.writeSync(fd, strChunk); });
28 29 30 31 32 33 34 35 36 37 38
let mode_sync; // Need to hijack fs.open/close to make sure that things // get closed once they're opened. fs._open = fs.open; fs._openSync = fs.openSync; fs.open = open; fs.openSync = openSync; fs._close = fs.close; fs._closeSync = fs.closeSync;
GitHub: nwjs/node
106 107 108 109 110 111 112 113 114
fs.utimes(pathType('foobarbaz'), atime, mtime, common.mustCall((err) => { expect_errno('utimes', 'foobarbaz', err, 'ENOENT'); // don't close this fd if (common.isWindows) { fd = fs.openSync(tmpdir.path, 'r+'); } else { fd = fs.openSync(tmpdir.path, 'r'); }
+ 3 other calls in file
43 44 45 46 47 48 49 50 51 52
'fs.unlinkSync("fs7.txt")'; tests['fs.sync.fstat'] = 'fs.writeFileSync("fs8.txt", "123", "utf8");' + 'fs.readFileSync("fs8.txt");' + 'fs.unlinkSync("fs8.txt")'; tests['fs.sync.fsync'] = 'fs.writeFileSync("fs9.txt", "123", "utf8");' + 'const fd = fs.openSync("fs9.txt", "r+");' + 'fs.fsyncSync(fd);' + 'fs.unlinkSync("fs9.txt")'; tests['fs.sync.ftruncate'] = 'fs.writeFileSync("fs10.txt", "123", "utf8");' + 'const fd = fs.openSync("fs10.txt", "r+");' +
+ 11 other calls in file
453 454 455 456 457 458 459 460 461 462 463 464
}; fs.open(nonexistentFile, 'r', 0o666, common.mustCall(validateError)); assert.throws( () => fs.openSync(nonexistentFile, 'r', 0o666), validateError ); }
42 43 44 45 46 47 48 49 50 51 52 53
stat = fs.statSync(filename); assert.equal(stat.size, 0); // ftruncateSync fs.writeFileSync(filename, data); var fd = fs.openSync(filename, 'r+'); stat = fs.statSync(filename); assert.equal(stat.size, 1024 * 16);
9 10 11 12 13 14 15 16 17 18 19 20 21
function saveStream() { var maxCount = 1024*1024; var array = new Uint32Array(maxCount*3); var count = 0; var file = fs.openSync('../data/g_edges.bin', 'w'); edgeDB.createValueStream() .on('data', function (data) {
732 733 734 735 736 737 738 739 740 741
} let stream let fd try { fd = fs.openSync(entry.absolute, 'w', mode) } catch (er) { return oner(er) } const tx = this.transform ? this.transform(entry) || entry : entry
GitHub: kyriosli/node-zip
342 343 344 345 346 347 348 349 350 351
* @param file a filename, or a fd, or a buffer * @returns {*} */ exports.unzip = function (file) { if (typeof file === 'string') { file = fs.openSync(file, 'r'); } if (typeof file === 'number') { return zipFile(fs.fstatSync(file).size, function (offset, length) { var buf = new Buffer(length);
192 193 194 195 196 197 198 199 200 201 202 203
return data.join("") + "\n"; } const buildAppFile = () => { const file = "src/App.tsx"; const fd = openSync(file, "w+"); const data = getAppData(); const buffer = Buffer.from(data); writeSync(fd, buffer, 0, buffer.length, 0); //write new data
170 171 172 173 174 175 176 177 178 179 180
} class TouchSync extends Touch { open () { try { this.onopen(null, fs.openSync(this.path, this.oflags)) } catch (er) { this.onopen(er) } }
+ 2 other calls in file
253 254 255 256 257 258 259 260 261 262 263
}); } function openSync(affixes) { var filePath = generateName(affixes, 'f-'); var fd = fs.openSync(filePath, RDWR_EXCL, 0600); deleteFileOnExit(filePath); return {path: filePath, fd: fd}; }
GitHub: ds-sampaio/ClicouChegouApp
263 264 265 266 267 268 269 270 271 272
opts.postfix = opts.postfix || '.tmp'; const discardOrDetachDescriptor = opts.discardDescriptor || opts.detachDescriptor; const name = tmpNameSync(opts); var fd = fs.openSync(name, CREATE_FLAGS, opts.mode || FILE_MODE); if (opts.discardDescriptor) { fs.closeSync(fd); fd = undefined; }
792 793 794 795 796 797 798 799 800 801
done() } let fd try { fd = fs.openSync(entry.absolute, getFlag(entry.size), mode) } catch (er) { return oner(er) } const tx = this.transform ? this.transform(entry) || entry : entry
+ 4 other calls in file
60 61 62 63 64 65 66 67 68 69 70
} function fchmod() { const fs = require('fs'); fs.writeFileSync('fs5.txt', '123', 'utf8'); const fd = fs.openSync('fs5.txt', 'r+'); fs.fchmod(fd, 100, () => { fs.unlinkSync('fs5.txt'); }); }
+ 17 other calls in file
46 47 48 49 50 51 52 53 54 55
try { try { fd = fs.openSync(opt.file, 'r+') } catch (er) { if (er.code === 'ENOENT') { fd = fs.openSync(opt.file, 'w+') } else { throw er } }
+ 3 other calls in file
fs.readFileSync is the most popular function in fs (2736 examples)