为什么这个Java代码没有用#跳过?

Nat*_*han 6 java file-io

我是一个新手,但我试图允许Java脚本读取的外部.txt文件能够在文件的开头有一些注释,以便其他人可以轻松编辑它并添加更多内容.但是如果文件包含#(为一行注释指定的符号),它只返回错误,即文件中存在"格式错误"(IOException - 所以它超过了第一个"IF"... )有人可以帮忙吗?

以下是代码部分,用于处理在脚本中先前调用的.txt文件中的注释行:

   while ((line = br.readLine()) != null) {
    line = line.trim();
    if (line.length() < 1 || line.charAt(0) == '#') { // ignore comments
     continue;
    }
    final String[] parts = line.split("=");
    if (parts.length != 2) {
     throw new IOException("Format error in file "
       + JLanguageTool.getDataBroker().getFromRulesDirAsUrl(getFileName())
       + ", line: " + line);
    }
Run Code Online (Sandbox Code Playgroud)

input.txt文件在第一行中断它:

#This is a Test
???|???=???
?????=??????
???????=??????
Run Code Online (Sandbox Code Playgroud)

这是实际的错误:

Caused by: java.io.IOException: Format error in file
Run Code Online (Sandbox Code Playgroud)

file:/ D:/ Documents ......./coherency.txt,line:#This is rule at rules.km.KhmerSimpleReplaceRule.loadWords(KhmerSimpleReplaceRule.java:165)at rules.km.KhmerSimpleReplaceRule.loadWords( KhmerSimpleReplaceRule.java:82)......还有33个

并且堆栈跟踪错误:

引起:java.io.IOException:文件中的格式错误[Ljava.lang.StackTraceElement; @ 1cb2795 at km.KhmerSimpleReplaceRule.loadWords(KhmereSimpleReplaceRule.java:169)

tig*_*ger 9

在您的第一个可见角色前面可能有一个UTF-8字节顺序标记.大多数编辑器都不会显示这些字符,因为它只预测内容的编码,Java不会删除UTF-8字节顺序标记(与UTF-16和32不同).如果确实存在UTF-8 BOM,您必须自己删除这三个字节.

有关更多详细信息,请参阅Java-Bug 6378911.