UTF8 텍스트 파일을 읽은 다음 true를 반환해야하는 equals()와 텍스트 비교를 시도하고 있습니다. 하지만 getBytes()는 differnt 값을 반환하므로 그렇지 않습니다.UTF8 파일을 읽고 문자열과 비교하십시오.
이 최소한의 예입니다
public static void main(String[] args) throws Exception {
System.out.println(Charset.defaultCharset()); // UTF-8
InputStream is = new FileInputStream("./myUTF8File.txt");
BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF8"));
String line;
while ((line = in.readLine()) != null) {
System.out.print(line); // mouseover
byte[] bytes = line.getBytes(); // [-17, -69, -65, 109, 111, 117, 115, 101, 111, 118, 101, 114]
String str = "mouseover";
byte[] bytesStr = str.getBytes(); // [109, 111, 117, 115, 101, 111, 118, 101, 114]
if (line.equals(str)) { // false
System.out.println("equal");
}
}
}
나는 문자열 line.readLine에서 UTF-16 convertet 것을 기대() 그 사실을 반환 같습니다. 이유를 알아낼 수 없습니다.
또한 이처럼'getBytes()'를 사용하지 마십시오. 플랫폼 기본 인코딩을 사용합니다. 이것은 보통 (보통) 나쁜 생각입니다. –