I have an Unicode ("Windows Notepad Unicode" or UTF-16LE) text file from which I read line like this:
FileInputStream is = new FileInputStream(cmdFile);
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-16LE"));
String line = reader.readLine();
Now I need to check whether line starts with a certain sequence of characters:
if (line.startsWith("[COMMAND]")) ...
But this returns false even if line actually "starts" with this sequence of characters.
When examining source code for startsWith
I can see that comparision is done character by character. But as far as I have read, Java actually represents strings internally with this particular encoding so why comparision fails? And what is the correct way to compare in this case?
One thing that comes in mind is converting String
to byte array with needed encoding and then comparing both byte arrays but that seems like a rather crude approach, is there more elegant way?