const fs = require('fs'); const path = require('path'); // Create folder fs.mkdir(path.join(__dirname, '/test'), {}, err => { if (err) throw err; console.log('Folder created...'); }); // Create and write to file fs.writeFile(path.join(__dirname, '/test', 'hello.txt'), 'Hello World!', err => { if (err) throw err; console.log('File written to...'); }); // Add text to an existing file - this will OVERRIDES the existing content in this file fs.writeFile(path.join(__dirname, '/test', 'hello.txt'), ' I love Node.js', err => { if (err) throw err; console.log('File written to...'); }); // Append content inside a file fs.appendFile(path.join(__dirname, '/test', 'hello.txt'), ' I love Node.js', err => { if (err) throw err; console.log('File written to...'); }); // Read file content fs.readFile(path.join(__dirname, '/test', 'hello.txt'), 'utf8', (err, data) => { if (err) throw err; console.log(data); }); // Rename file fs.rename(path.join(__dirname, '/test', 'hello.txt'), path.join(__dirname, '/test', 'helloworld.txt'), (err) => { if (err) throw err; console.log('File renamed...'); });
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter