Last active
December 11, 2015 07:09
-
-
Save sebastienblanc/4564581 to your computer and use it in GitHub Desktop.
basic open / write / close
This file contains hidden or 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
/** | |
* script run by nodeJ : | |
* var fs = require('fs'); | |
* fs.writeFile("/tmp/test", "Hey there!", function(err) { | |
* if(err) { | |
* console.log(err); | |
* } else { | |
* console.log("The file was saved!"); | |
* } | |
* }); | |
*/ | |
public Fs(GlobalObject globalObject) { | |
super(globalObject); | |
final GlobalObject globalObject1 = globalObject; | |
Binding.setProperty(this, "Stats", new Stats(globalObject)); | |
Binding.setProperty(this, "open", new AbstractNativeFunction(globalObject) { | |
@Override | |
public Object call(ExecutionContext context, Object self, Object... args) { | |
context.call((JavascriptFunction) args[3],self,null,args[0]); | |
return null; | |
} | |
}); | |
Binding.setProperty(this, "write", new AbstractNativeFunction(globalObject) { | |
@Override | |
public Object call(ExecutionContext context, Object self, Object... args) { | |
try{ | |
// Create file | |
FileWriter fstream = new FileWriter(args[0].toString()); | |
BufferedWriter out = new BufferedWriter(fstream); | |
out.write(args[1].toString()); | |
//Close the output stream | |
out.close(); | |
}catch (Exception e){//Catch exception if any | |
System.err.println("Error: " + e.getMessage()); | |
} | |
context.call((JavascriptFunction) args[5],self,null,10); | |
return null; | |
} | |
}); | |
Binding.setProperty(this, "close", new AbstractNativeFunction(globalObject) { | |
@Override | |
public Object call(ExecutionContext context, Object self, Object... args) { | |
return null; | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment