NodeJS: BASIC: Mengelola File: Baca, Tulis, Buat, Hapus
const fs = require('fs');
// Reading Files
fs.readFile('./doc/blog11.txt', (error, data) => {
if (error) {
console.log(error);
}
console.log(data.toString());
});
console.log('Last line here!!!');
//Writing Files
fs.writeFile('./doc/blog1.txt', 'console.log("data")', () => {
console.log('File blog.txt was written...');
});
fs.writeFile('./doc/blog5.txt', "TextButton(\nonTap: (){}),", () => {
console.log('Create new file is: blog2.txt');
});
// Create and Delete Directorie
if (!fs.existsSync('./assets')) {
fs.mkdir('./assets', (err) => {
if (err) {
console.log('Have Error ${err}');
}
console.log('Folder was created.....');
});
} else {
fs.rmdir('./assets', (err) => {
if (err) {
console.log('Have error to Remove Directory:: ${err}');
}
console.log('Folder was Deleted');
});
}
// Delete File toggle Write File
if (fs.existsSync('./doc/deleteme.txt')) {
fs.unlink('./doc/deleteme.txt', (err) => {
if (err) {
console.log(err);
return
}
console.log('File was deleted');
});
} else {
fs.writeFile('./doc/deleteme.txt', 'Create new File', () => {
console.log('File was created');
});
}
Sore Sardine