This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fs = require('fs'); | |
path = 'data/myfile.txt'; | |
fs.exists(path, function(exists){ | |
if(exists){ | |
console.log('yes file exists...'); | |
} else { | |
console.log('It doesnt exists'); | |
} | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fs.appendFile('myfile.txt', 'this content goes at end of the file', function(err){ | |
if(err) { | |
console.log('Error is thrown', err); throw err; | |
} | |
console.log('data is appended !!'); | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fs = require("fs"); | |
path = "data/myfile.txt"; | |
data = "This content is written from Node program"; | |
result =fs.writeFileSync(path, data); | |
console.log('written contents are : ' + result); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fs = require("fs"); | |
path = "myfile.txt"; | |
data = "This content is written from Node program"; | |
fs.writeFile(path, data, function(error){ | |
if error | |
console.error("error: " + error.message); | |
else | |
console.log("Successful Write to " + path); | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
res= <destination file stream>; // It can be standard io, another file stream, etc | |
readStream = fs.createReadStream(filename); | |
readStream.on('open', function(){ | |
readStream.pipe(res); | |
}); | |
readStream.on('error', function(err){ | |
res.end(err); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//read it synchronously | |
content= fs.readFileSync('package.json', 'utf-8'); | |
console.log(content); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fs= require(‘fs’) | |
//read it asynchronously | |
fs.readFile('package.json', function(err, res){ | |
if(err){ | |
console.log('Error while reading the file content '); | |
} | |
console.log('file contents- ' + res) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"name":"ajduke"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Default behaviour.... | |
{"myField":"value1","myAnotherField":"value2"} | |
Fields with lower case and dashes... | |
{"my-field":"value1","my-another-field":"value2"} | |
Fields with lower case and dashes... | |
{"My Field":"value1","My Another Field":"value2"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package in.ajduke.ap013; | |
import com.google.gson.FieldNamingPolicy; | |
import com.google.gson.Gson; | |
import com.google.gson.GsonBuilder; | |
public class Example34 { | |
public static void main(String[] args) { | |
Gson gson = new Gson(); |