Skip to content

Instantly share code, notes, and snippets.

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');
}
})
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 !!');
})
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);
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);
})
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);
});
//read it synchronously
content= fs.readFileSync('package.json', 'utf-8');
console.log(content);
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)
})
{"name":"ajduke"}
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"}
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();