Skip to content

Instantly share code, notes, and snippets.

@sebastienblanc
Last active December 11, 2015 07:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebastienblanc/4564581 to your computer and use it in GitHub Desktop.
Save sebastienblanc/4564581 to your computer and use it in GitHub Desktop.
basic open / write / close
/**
* 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