Create File If Missing
NodeJS
Easy
3 views
Problem Description
If file does not exist, create it with 'meetcode' and print CREATED/EXISTS.
Input Format
stdin: file path.
Output Format
Print one word.
Constraints
Use fs.existsSync.
Official Solution
const fs = require('fs');
const p = fs.readFileSync(0, 'utf8').trim();
if (!p) process.exit(0);
if (fs.existsSync(p)) {
console.log('EXISTS');
} else {
fs.writeFileSync(p, 'meetcode', 'utf8');
console.log('CREATED');
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!