我有一个基于 java 的应用程序,它读取一个文件并在基于代码镜像的编辑器中显示该文件。java代码读取用户选择的文件并将其转换为字符串。然后使用“editor.setvalue”将字符串传递给编辑器。问题是任何硬编码的字符串都可以正常显示。但是如果它是从文件中读取的,那么我会得到一个错误
Caused by: netscape.javascript.JSException: SyntaxError: Unexpected EOF
我能够正确打印文件,没有问题。
这就是我在java中尝试做的事情。
String sb = FileUtils.readFileToString(file);
webengine.executeScript("editor.setValue('" + sb + "');");
“webengine.executescript”是一种 javafx 方法,用于调用 webengine 中呈现的 html 可见的 javascript 函数。
我的代码镜像代码。
<form>
<textarea id="code" name="code">
</textarea>
</form>
<script>
var editor = CodeMirror.fromTextArea(document.getElementById("code"), {
lineNumbers : true,
matchBrackets : true,
mode : "text/x-java",
theme : "theme_value",
indentUnit : 4,
gutter: true
});
</script>
我无法理解是哪些角色导致了这个问题。我尝试使用“+”运算符添加两个字符串,并显示添加的字符串。但是,当我在字符串之间插入换行符 "\n" 时,我开始收到相同的错误。我尝试用 替换换行符
,但没有太大帮助,因为它们在没有换行的情况下按原样显示。