- 1 year ago
How to determine the currently active code page from a Java console application on Windows?
Comment
1 / 1
In a Java console application on Windows, you can determine the currently active code page using the following code:
javaimport java.io.IOException;
import java.io.InputStreamReader;
import java.io.BufferedReader;
public class ActiveCodePage {
public static void main(String[] args) {
String codePage = getActiveCodePage();
System.out.println("Active Code Page: " + codePage);
}
public static String getActiveCodePage() {
try {
Process process = Runtime.getRuntime().exec("chcp");
BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = reader.readLine();
if (line != null && line.matches("Active code page: (\\d+)")) {
return line.substring(line.lastIndexOf(" ") + 1);
}
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
This code runs the chcp
command in the command prompt, which displays the active code page. It then captures the output of the command and extracts the code page value from it.
Please note that the code page is specific to the console environment, and it may not necessarily represent the code page used by other parts of the Java application.