admin管理员组文章数量:1022705
I am interested in a portable way of writing a string to standard output, without implicit newlines added to the end, ideally forcing encoding to UTF-8, that works with either of
- jrunscript (from any JDK)
- Rhino
- node.js
My current code tries to detect where it is running, then uses a platform-specific write method:
if (typeof process !== "undefined") { // assume node.js
var log = function(string) {process.stdout.write(string);};
}
else if (typeof println == "undefined") { // assume rhino
var log = function(string) {java.lang.System.out.write(java.lang.String(string).getBytes("utf-8"));};
}
else { // assume jrunscript
var log = function(string) {java.lang.System.out.print(string);};
}
log("X");
log("Y");
It should result in:
XY
Can this be done better?
For jrunscript, I had been using function print
, but that changed its behavior in JDK-8 on behalf of JDK-8021773.
I am interested in a portable way of writing a string to standard output, without implicit newlines added to the end, ideally forcing encoding to UTF-8, that works with either of
- jrunscript (from any JDK)
- Rhino
- node.js
My current code tries to detect where it is running, then uses a platform-specific write method:
if (typeof process !== "undefined") { // assume node.js
var log = function(string) {process.stdout.write(string);};
}
else if (typeof println == "undefined") { // assume rhino
var log = function(string) {java.lang.System.out.write(java.lang.String(string).getBytes("utf-8"));};
}
else { // assume jrunscript
var log = function(string) {java.lang.System.out.print(string);};
}
log("X");
log("Y");
It should result in:
XY
Can this be done better?
For jrunscript, I had been using function print
, but that changed its behavior in JDK-8 on behalf of JDK-8021773.
- I think you will need platform-specific code – Bergi Commented Mar 19, 2014 at 17:02
- I have similar code in my project. Note that you'll want to test on Nashorn too. (Java 8 bundles Nashorn, not Rhino.) – Seth Tisue Commented Mar 26, 2014 at 17:03
- @SethTisue, thanks for the hint. In fact upgrading JDK to 8 was what got me thinking about this again. Also, Rhino and its (pre-8) JDK flavor already needed to be distinguished to this respect. – Gunther Commented Mar 26, 2014 at 18:33
1 Answer
Reset to default 3I am interested in a portable way of writing a string to standard output, without implicit newlines added to the end, ideally forcing encoding to UTF-8, that works with either of...
there is none,since javascript doesnt have "system apis". JS core is limited and you need to use the api available within the environment javascript engine is running in.
My current code tries to detect where it is running, then uses a platform-specific write method. But I'd rather get rid of that.
that's the only thing you can do.
EDIT : the javascript spec is here : http://es5.github.io/ and it's short,there is 0 notion of standard output or standard input because there is no javascript standard library.
I am interested in a portable way of writing a string to standard output, without implicit newlines added to the end, ideally forcing encoding to UTF-8, that works with either of
- jrunscript (from any JDK)
- Rhino
- node.js
My current code tries to detect where it is running, then uses a platform-specific write method:
if (typeof process !== "undefined") { // assume node.js
var log = function(string) {process.stdout.write(string);};
}
else if (typeof println == "undefined") { // assume rhino
var log = function(string) {java.lang.System.out.write(java.lang.String(string).getBytes("utf-8"));};
}
else { // assume jrunscript
var log = function(string) {java.lang.System.out.print(string);};
}
log("X");
log("Y");
It should result in:
XY
Can this be done better?
For jrunscript, I had been using function print
, but that changed its behavior in JDK-8 on behalf of JDK-8021773.
I am interested in a portable way of writing a string to standard output, without implicit newlines added to the end, ideally forcing encoding to UTF-8, that works with either of
- jrunscript (from any JDK)
- Rhino
- node.js
My current code tries to detect where it is running, then uses a platform-specific write method:
if (typeof process !== "undefined") { // assume node.js
var log = function(string) {process.stdout.write(string);};
}
else if (typeof println == "undefined") { // assume rhino
var log = function(string) {java.lang.System.out.write(java.lang.String(string).getBytes("utf-8"));};
}
else { // assume jrunscript
var log = function(string) {java.lang.System.out.print(string);};
}
log("X");
log("Y");
It should result in:
XY
Can this be done better?
For jrunscript, I had been using function print
, but that changed its behavior in JDK-8 on behalf of JDK-8021773.
- I think you will need platform-specific code – Bergi Commented Mar 19, 2014 at 17:02
- I have similar code in my project. Note that you'll want to test on Nashorn too. (Java 8 bundles Nashorn, not Rhino.) – Seth Tisue Commented Mar 26, 2014 at 17:03
- @SethTisue, thanks for the hint. In fact upgrading JDK to 8 was what got me thinking about this again. Also, Rhino and its (pre-8) JDK flavor already needed to be distinguished to this respect. – Gunther Commented Mar 26, 2014 at 18:33
1 Answer
Reset to default 3I am interested in a portable way of writing a string to standard output, without implicit newlines added to the end, ideally forcing encoding to UTF-8, that works with either of...
there is none,since javascript doesnt have "system apis". JS core is limited and you need to use the api available within the environment javascript engine is running in.
My current code tries to detect where it is running, then uses a platform-specific write method. But I'd rather get rid of that.
that's the only thing you can do.
EDIT : the javascript spec is here : http://es5.github.io/ and it's short,there is 0 notion of standard output or standard input because there is no javascript standard library.
本文标签: nodejsHow to write a string to standard output from JavascriptStack Overflow
版权声明:本文标题:node.js - How to write a string to standard output from Javascript - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745563388a2156299.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论