Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
341 views
in Technique[技术] by (71.8m points)

java - Two identical Strings are not equal(Not pointer/reference mistake)

I read a line from a file:

KatalogObrazków 1 32

Means that I should look for data in:

C:UsersNAME_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:UsersR/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 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...