我是一个新手,但我试图允许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文件在第一行中断它:
Run Code Online (Sandbox Code Playgroud)#This is a Test ???|???=??? ?????=?????? ???????=??????
这是实际的错误:
Run Code Online (Sandbox Code Playgroud)Caused by: java.io.IOException: Format error in filefile:/ 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)