开发者

How to convert a string to an inputstream in Bioclipse javascript editor?

开发者 https://www.devze.com 2023-01-16 07:24 出处:网络
I\'m trying to save an a string to a file in with a javascript in the Bioclipse workbench, by using ui.save( \"filename\", \"my string\" );

I'm trying to save an a string to a file in with a javascript in the Bioclipse workbench, by using

ui.save( "filename", "my string" );

...but get an error that ui.save takes only an inputst开发者_如何学Cream as the second parameter. How can I convert a string to an inputstream in the Bioclipse javascript context?

(Btw, I think Bioclipse uses the Rhino Javascript implementation)


In this situation we have to fall back to Java.

You are trying to call the method ui.save which according to man ui.save looks like this:

> man ui.save
---------------------------------------------
ui.save(String filePath, InputStream content)
---------------------------------------------
Save the content of the InputStream to the given path.

So this method wants an InputStream. Rhino allows us to instantiate Java objects. This can probably be made much nicer...

var stream = new java.io.ByteArrayInputStream(
                      new java.lang.String("Example String").getBytes("UTF-8") );

And then we call the method with this stream, (and an existing path where to save the file)

ui.save("/test/test.txt", stream);


Here's a page describing java interop using Rhino: http://www.mozilla.org/rhino/ScriptingJava.html

0

精彩评论

暂无评论...
验证码 换一张
取 消