Why doesn't this Java code skip C # lines?

I'm a little newbie, but I'm trying to resolve an external .txt file that is read by a Java script, be able to add comments at the beginning of the file so that others can easily edit it. and add more to it. But if the file contains # (the character designated for the line, which is a comment), it simply returns an error that there is a β€œFormat error in file” (an IOException exception, so it passes this first β€œIF” ...) Maybe someone any help?

Here's the piece of code that processes comment lines from a .txt file that was called earlier in the script:

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); } 

The input.txt file splits it into the first line:

 #This is a Test αž²αŸ’αž™|αž±αŸ’αž™=αž’αŸ„αž™ αž€αŸ†αž–αžŸαŸ‹=αž€αž˜αŸ’αž–αžŸαŸ‹ αž€αž˜αŸ’αž“αžΆαž‰αŸ‹=αž€αŸ†αžŽαžΆαž‰αŸ‹ 

And here is the actual error:

 Caused by: java.io.IOException: Format error in file 

File: / D: / documents ....... / coherency.txt, line: # This is a test in rules.km.KhmerSimpleReplaceRule.loadWords (KhmerSimpleReplaceRule.java:165) in rules.km.KhmerSimpleReplaceRule.loadWords (KhmerSimpleReplaceRule. java: 82) ... 33 more info

And the stack trace error:

Called: java.io.IOException: Format error in file [Ljava.lang.StackTraceElement; @ 1cb2795 at km.KhmerSimpleReplaceRule.loadWords (KhmereSimpleReplaceRule.java: 169)

+6
java file-io
source share
5 answers

Before your first visible character, there may be a UTF-8 byte order character. Most editors will not show these characters, since they only predict the encoding of the contents, and Java does not remove the byte order mark of UTF-8 (unlike UTF-16 and 32). If the UTF-8 specification really exists, you will have to delete these three bytes yourself.

See Java-Bug 6378911 for more details .

+9
source share

This should work if there are no spaces. You can try this code.

 if (line.trim().startsWith("#") { // ignore comments continue; } 
+4
source share

This should work if # is not really the first character without a space in the line (or you have a line without a comment somewhere with no more than one = ).

I can only suggest that you show us the complete exception, which will include the actual line of violation. You can also do this:

 + ", line: [" + line + "]"); 

therefore, you are sure that there are no leading or trailing spaces. In addition, the output of line.codePointAt(0) in the exception also - it may be a problem with the language / incorrect Unicode code.

You might also consider making your code more flexible so that comments also appear at the end of lines. This is a simple question: delete everything from the first # to the end of the line before trimming and allow things like:

 password = xyzzy # super sekrit sauce from zork 
+2
source share

At first, your code seems to be correct ... I see several options:

The stacktrace element and input file may help ...

+1
source share

Try:

  line.indexOf('#') == 0 

Or you could try:

  line.substring(0,0).equals("#") 

Or show the stack trace and find the actual answer.

+1
source share

All Articles