Spidermonkey js shell中`read`/`snarf`的对面,函数是将字符串写入文件?
Opposite of `read`/`snarf` in Spidermonkey js shell, function to write a string to a file?
Spidermonkey 的 js
shell 有一个很好的功能,称为 read
/snarf
,可以从磁盘读取文件。是否有 write 文件的等效方法? documentation 中没有类似的内容,但也许有人知道一些未记录的方法来执行此操作。
我正在寻找一种从交互式 js
会话中获取大字符串的方法。在 browser/Firebug 中,我可以使用 copy
将字符串复制到剪贴板,或将其添加到 DOM 中进行复制。在节点中,我会使用 fs
来写一个文件。
如果它对您有用,我通过将终端输出重定向到文件来解决这个问题。所以我所做的就是使用 SpiderMonkey shell 中的 'print' 命令(它将打印到终端),然后将终端输出重定向到一个文件。
示例:
//from SpiderMonkey
print('Test - write to file');
//terminal(assume script is called test.js)
./js test.js >> testFile.txt
我正在使用 Ubuntu,但这可以在所有系统上完成。希望对您有所帮助:-)
您可以像这样使用 shell 公开的 redirect
和 putstr
函数:
const previous = redirect('path/to/your/file')
putstr('stuff to be written')
redirect(previous) // restore the redirection to stdout
或者您可以使用 os.file.writeTypedArrayToFile
来编写类型化数组。
Spidermonkey 的 js
shell 有一个很好的功能,称为 read
/snarf
,可以从磁盘读取文件。是否有 write 文件的等效方法? documentation 中没有类似的内容,但也许有人知道一些未记录的方法来执行此操作。
我正在寻找一种从交互式 js
会话中获取大字符串的方法。在 browser/Firebug 中,我可以使用 copy
将字符串复制到剪贴板,或将其添加到 DOM 中进行复制。在节点中,我会使用 fs
来写一个文件。
如果它对您有用,我通过将终端输出重定向到文件来解决这个问题。所以我所做的就是使用 SpiderMonkey shell 中的 'print' 命令(它将打印到终端),然后将终端输出重定向到一个文件。
示例:
//from SpiderMonkey
print('Test - write to file');
//terminal(assume script is called test.js)
./js test.js >> testFile.txt
我正在使用 Ubuntu,但这可以在所有系统上完成。希望对您有所帮助:-)
您可以像这样使用 shell 公开的 redirect
和 putstr
函数:
const previous = redirect('path/to/your/file')
putstr('stuff to be written')
redirect(previous) // restore the redirection to stdout
或者您可以使用 os.file.writeTypedArrayToFile
来编写类型化数组。