我从文件中读取一行:
KatalogObrazków132
意味着我应该寻找以下数据:
C:\Users\NAME_OF_THE_USER/KatalogObrazków
所以我这样做,但有可怕的事情发生.在splitLine [0]中,我有一个单词“KatalogObrazków”但是计算机说“KatalogObrazków”.equals(splitLine [0])是假的,分裂线后没有左边的splitLine [0]空格.请看下面的代码.
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();
输出:
KatalogObrazków
false
false
C:\Users\R/KatalogObrazków
编辑:
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
解决方法:
您可能在文件开头遇到了UTF-BOM.
它是隐形的,因为大多数编辑都隐藏它.很邪恶,对吧?
标签:java,string,equals
来源: https://codeday.me/bug/20190825/1715519.html