5

I read a line from a file:

KatalogObrazków 1 32

Means that I should look for data in:

C:\Users\NAME_OF_THE_USER/KatalogObrazków

and so I do it, but there is horrible thing going on. In splitLine[0] I have a word "KatalogObrazków" but then the computer says that "KatalogObrazków".equals(splitLine[0]) is false, there is no whitespace arround splitLine[0] left after splitting line. Please have a look at the code below.

    BufferedReader br = new BufferedReader(new FileReader(path));
    String line;
    String[] splitLine;
    if ((line = br.readLine()) != null) {
        splitLine = line.split(" ");
        System.out.println(splitLine[0]);//1st line of output
        System.out.println("KatalogObrazków".equals(splitLine[0]));//these are not EQUAL!!!!!??? WHY?
        imageDirectoryPath = System.getProperty("user.home")+"/" + splitLine[0];
        System.out.println(new File(imageDirectoryPath).exists());
        delay = Integer.parseInt(splitLine[1]);
        fontSize = Integer.parseInt(splitLine[2]);
    }
    br.close();

Output:

KatalogObrazków
false
false
C:\Users\R/KatalogObrazków

EDIT:

System.out.println();
            for (char c : splitLine[0].toCharArray())
                System.out.print((int) c + " ");
            System.out.println();
            for (char c : "KatalogObrazków".toCharArray())
                System.out.print((int) c + " ");
            System.out.println();

GOT ME:

65279 75 97 116 97 108 111 103 79 98 114 97 122 107 243 119 
75 97 116 97 108 111 103 79 98 114 97 122 107 243 119 
Yoda
  • 17,363
  • 67
  • 204
  • 344
  • 4
    If you use `getBytes()` with the right encoding, do you get the same array of bytes for both? They might be invisible characters. – Alexis C. May 08 '14 at 20:20
  • 2
    Try dumping the integer value of every char of both strings to know where the difference is: `for (char c : s.toCharArray()) System.out.println((int) c);` – JB Nizet May 08 '14 at 20:22
  • @JBNizet I posted edit. Yeah there is invisible number at the beginning. The problem is I am not sure what coding will be used in the future, but it will be probably UTF-8. My workspace is also UTF-8. System Windows 8.1 – Yoda May 08 '14 at 20:27
  • The "invisible number" is a byte order marker (0xFEFF) and should generally be treated as whitespace. – Hot Licks May 08 '14 at 20:31
  • 2
    That's a BOM. I don't know who had this awful idea to add a BOM at the beginning of files. You'd better remove it. Good editors allow saving a UTF8 file without BOM. – JB Nizet May 08 '14 at 20:31
  • [http://stackoverflow.com/questions/17540031/junk-characters-while-reading-text-file-in-java][1] [1]: http://stackoverflow.com/questions/17540031/junk-characters-while-reading-text-file-in-java This may help – Nielarshi May 08 '14 at 20:38

1 Answers1

7

You may have encountered the UTF-BOM at the beginning of your file.

http://en.wikipedia.org/wiki/Byte_order_mark

It's invisible because most editors hide it. Pretty evil, huh?

Asaph
  • 159,146
  • 25
  • 197
  • 199