I'm executing a Windows command and need to parse the results output, and compare part of the text with a string previously stored in java code.
But apparently the charset mismatch prevents the equals to return true.
Thats my code:
ProcessBuilder pb = new ProcessBuilder();
pb.command("systeminfo");
Process shell = pb.start();
InputStream shellIn = shell.getInputStream();
InputStreamReader reader = new InputStreamReader(shellIn, "Cp1252");
BufferedReader br = new BufferedReader(reader);
String sCurrentLine;
while((sCurrentLine = br.readLine()) != null) {
// ... omitting parse of sCurrentLine for brevity
System.out.println("DOS String:" + sCurrentLine);
System.out.println("JAVA String: "+ Versão");
System.out.println("Versão".equals(sCurrentLine));
}
And my output will be: (Commandline window):
Windows String: Versão
JAVA String: VersÒo
false
To a text file:
Windows String: VersÒo
JAVA String: Versão
false
I've found a couple of similar issues here in stackoverflow but none of the anwsers worked for me.
converting String from Windows charset to UTF 8 in Java
Converting from Windows 1252 to UTF8 in Java: null characters with CharsetDecoder/Encoder
How to parse a string that is in a different encoding from java
chcp
to show the codepage that a console window is using, And that is not something like 1252, but 850 (an old DOS code page) on my computer. Maybe you should use that in yourInputStreamReader
. – FrankPl