Java 如何格式化从.txt文件中读取的文本

Java 如何格式化从.txt文件中读取的文本,java,Java,我正在尝试从文件中读取文本,然后使用String.format()对其进行格式化;。该文件的内容如下所示 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="../plugin/codemirror/lib/codemirror.js"></script> <link rel="stylesheet" href=

我正在尝试从文件中读取文本,然后使用String.format()对其进行格式化;。该文件的内容如下所示

<!DOCTYPE html>
<html>

<head>
    <meta charset="UTF-8">

    <script src="../plugin/codemirror/lib/codemirror.js"></script>
    <link rel="stylesheet" href="../plugin/codemirror/lib/codemirror.css">
    <script src="../plugin/codemirror/mode/%1s/%1s.js"></script>
</head>

<body>
    <script>
        const editor = CodeMirror(document.body, {
            lineNumbers: true,
            mode: "%1s",
        });

        function changeEditorSize(width, height){
            editor.setSize(width, height);
        }
    </script>
</body>
</html>

然后,当字符串中明显有“%1s%”时,我得到错误“java.util.MissingFormatArgumentException:格式说明符“%1s”。有人知道为什么会发生这种情况吗?

String.format
使用
$
指定参数索引


您需要
%1$s

您希望这段代码能做什么“String.format(content,“javascript”);”?格式化程序不是这样工作的。我希望它将“%1s”的所有实例都替换为“javascript”,当然您会这样做。您正在用第一个
%1s
用尽您的参数。您可以使用
content.replace(“%1s”,“javascript”)
String.format(content, "javascript");