Search code examples
javascriptjavarhino

Write files in javascript


I use JavaScript as scripting language to a Java program that I'm developing. I need to print in a file certain data; online I read that I need require a library.

var fs = require("fs")

But when Java compile the code returns this error:

ReferenceError: "require" is not defined in lib.js at line number 1

I also tried to use import and load() but nothing changed.

So how can I write files with JavaScript used as script?


Solution

  • You should be able to access the Java standard library, so use their classes. For example:

    var writer = new java.io.FileWriter("filename.txt");
    writer.append("contents");
    writer.close();