A
A
Alexander Interesting2021-07-24 14:22:00
Java
Alexander Interesting, 2021-07-24 14:22:00

How to print output from Process in normal encoding?

I run a bat file and take an outputstream from it with

Process pro = Runtime.getRuntime().exec(file.toString());
OutputStream stdin = pro.getOutputStream();
InputStream stdout = pro.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stdout));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));

What encoding should be entered as the second argument in InoutStreamReader and what JVM arguments should be written in order to get Russian characters in the console?

The code:
Process pro = Runtime.getRuntime().exec(file.toString());
OutputStream stdin = pro.getOutputStream();
InputStream stdout = pro.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stdout, "CP866"));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(stdin));
new Thread(() -> {
while (true){
    Scanner sc = new Scanner(System.in);
    String s = sc.nextLine();
    try {
         writer.write(s+"\n");
         writer.flush();
     }catch (IOException e){
         e.printStackTrace();
     }
}
}).start();
byte line;
while ((line = (byte) reader.read()) != -1) System.out.print(new String(new byte[]{line}));

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question