Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

nodejs: Basic: managing file: Read, Write, Create, Delete

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(
onTap: (){}),", () => {
    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');
    });
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: pizza form validation jquery 
Javascript :: how to divide a month into weeks in moment js 
Javascript :: cast uint to address in solidity 
Javascript :: promise limit time 
Javascript :: mongoose findbyidandupdate or findoneandupdate 
Javascript :: javascipt 
Javascript :: theme ui with react 17 
Javascript :: javascript read all cookies 
Javascript :: maximum product of word lengths leetcode solution 
Javascript :: phaser place on circles 
Javascript :: phaser add animation event 
Javascript :: phaser pause all animations 
Javascript :: lookbehind alternative regex 
Javascript :: permissions in chrome extension javascript 
Javascript :: file size to string js 
Javascript :: JavaScript date format 2 
Javascript :: iterate over array of html elements 
Javascript :: nodelist example 
Javascript :: learn javascript 
Javascript :: fibonacci sequence array 
Javascript :: 404 responses in express 
Javascript :: node js mongodb update nested object 
Javascript :: vue sidebar 
Javascript :: Update an object as state with React hooks 
Javascript :: how to create scroll to top button in reactjs example code 
Javascript :: mvc asp.net partial view from js 
Javascript :: javascript how to select a array 
Javascript :: working of a recursive function 
Javascript :: alertify js examples 
Javascript :: array js 
ADD CONTENT
Topic
Content
Source link
Name
1+1 =