关于java管道通信中设置了编码但仍然出现乱码的问题

为什么BufferedWriter的构造方法中指明了UTF-8编码后输出的还是乱码?

BufferedReader br = null;
BufferedWriter bw = null;
br = new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8),510241024);
bw = new BufferedWriter(new OutputStreamWriter(process.getOutputStream(), StandardCharsets.UTF_8));

"C:\Program Files\Java\jdk-17\bin\java.exe" -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:8150,suspend=y,server=n -javaagent:C:\Users\Administrator\AppData\Local\JetBrains\IdeaIC2022.3\captureAgent\debugger-agent.jar -Dfile.encoding=UTF-8 -classpath "F:\java\projects\Zip Crack\out\production\Zip Crack;F:\java\idea\IntelliJ IDEA Community Edition 2022.3.2\lib\idea_rt.jar" GUI
已连接到目标 VM, 地址: ''127.0.0.1:8150',传输: '套接字''
Microsoft Windows [�汾 10.0.19045.2006]
(c) Microsoft Corporation����������Ȩ����

F:\java\projects\Zip Crack>bz.exe x -o:E:\�?】应用软件集\chatgpt\ E:\�?】应用软件集\chatgpt\mima.zip
bz 7.30(Beta,x64) - Bandizip console tool. Copyright(C) 2022 Bandisoft

与目标 VM 断开连接, 地址为: ''127.0.0.1:8150',传输: '套接字''

进程已结束,退出代码0

完整代码:

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.io.*;
import java.nio.charset.StandardCharsets;

public class GUI extends JFrame {
    public GUI(){
        try {
            UIManager.setLookAndFeel(javax.swing.plaf.nimbus.NimbusLookAndFeel.class.getName());
        } catch (ClassNotFoundException | InstantiationException | IllegalAccessException |
                 UnsupportedLookAndFeelException e) {
            throw new RuntimeException(e);
        }
        Container co = getContentPane();
        setLayout(new GridLayout(5,1));
        JPanel file_path_panel = new JPanel();
        JLabel file_path_label = new JLabel("压缩包路径");
        JButton file_path_set_button = new JButton("打开...");
        JPanel out_path_panel = new JPanel();
        JPanel two_buttons_panel = new JPanel(new GridLayout(2,1));
        JLabel out_path_label = new JLabel("导出路径");
        JButton out_path_button = new JButton("解压到...");
        JButton out_path_this_button = new JButton("解压到当前文件夹");
        JTextArea jta = new JTextArea();
        jta.setLineWrap(true);
        JButton start_crack = new JButton("开始");
        JPanel crack_mode = new JPanel(new GridLayout(1,7));
        JCheckBox is_number = new JCheckBox("数字");
        JCheckBox is_alphabet = new JCheckBox("小写字母");
        JCheckBox is_alohabet_toUpperCase = new JCheckBox("大写字母");
        JCheckBox is_symbol = new JCheckBox("特殊字符");
        JTextField key_start_number = new JTextField();
        key_start_number.addKeyListener(new ControlTheInput());
        JLabel bolang = new JLabel("~");
        JTextField key_end_number = new JTextField();
        key_end_number.addKeyListener(new ControlTheInput());
        start_crack.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        ProcessBuilder builder = new ProcessBuilder("cmd.exe");
                        builder.redirectErrorStream(true);
                        Process process = null;
                        try {
                            process = builder.start();
                        } catch (IOException ex) {
                            throw new RuntimeException(ex);
                        }
                        BufferedReader br = null;
                        BufferedWriter bw = null;
                        br = new BufferedReader(new InputStreamReader(process.getInputStream(), StandardCharsets.UTF_8),510241024);
                        bw = new BufferedWriter(new OutputStreamWriter(process.getOutputStream(), StandardCharsets.UTF_8));
//                        try {
//                            process.waitFor();
//                        } catch (InterruptedException ex) {
//                            throw new RuntimeException(ex);
//                        }

                        try {
                            bw.write("bz.exe x -o:"+out_path_label.getText()+" "+file_path_label.getText()+"\n");
                            bw.flush();
                            String s;
                            while ((s = br.readLine()) != null){
                                System.out.println(s);
//                                jta.append(s);
                            }
//                            bw.write("y\n");
//                            bw.flush();
//                            while ((s = br.readLine()) != null){
//                                System.out.println(s);
//                                jta.append(s);
//                            }
                        } catch (IOException ex) {
                            throw new RuntimeException(ex);
                        }

                        KeyDictionary kd = new KeyDictionary();
                        for(int i = Integer.parseInt(key_start_number.getText());i <= Integer.parseInt(key_end_number.getText());i++){
                            kd.init(is_number.isSelected(),is_alphabet.isSelected(),is_alohabet_toUpperCase.isSelected(),is_symbol.isSelected(),i);
                            String end_key = kd.get_end_key();
                            while (true){
                                String key = kd.GetKey();
                                try {
                                    bw.write(key+"\n");
                                    bw.flush();
                                    String c = null;
                                    if (!read(c,jta,br)){
                                        break;
                                    }
                                } catch (IOException ex) {
                                    throw new RuntimeException(ex);
                                }
                                if (key.equals(end_key)){
                                    break;
                                }
                            }
                        }
                        try {
                            br.close();
                            bw.close();
                        } catch (IOException ex) {
                            throw new RuntimeException(ex);
                        }

                    }
                }).start();
            }
        });

        file_path_set_button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser file_path_chooser = new JFileChooser();
                file_path_chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
                int i = file_path_chooser.showOpenDialog(getContentPane());
                if(i == JFileChooser.APPROVE_OPTION) {
                    File zip_file = file_path_chooser.getSelectedFile();
                    file_path_label.setText(zip_file.getAbsolutePath());
                }
            }
        });
        out_path_button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser out_path_chooser = new JFileChooser();
                out_path_chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                int i = out_path_chooser.showOpenDialog(getContentPane());
                if(i == JFileChooser.APPROVE_OPTION) {
                    File out_path = out_path_chooser.getSelectedFile();
                    out_path_label.setText(out_path.getAbsolutePath());
                }
            }
        });
        out_path_this_button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String[] split = file_path_label.getText().split("\\\\");
                String new_file_path = file_path_label.getText().replace(split[split.length-1],"");
                out_path_label.setText(new_file_path);
            }
        });
        co.add(jta);
        file_path_panel.add(file_path_label,BorderLayout.CENTER);
        file_path_panel.add(file_path_set_button,BorderLayout.EAST);
        co.add(file_path_panel);
        two_buttons_panel.add(out_path_button);
        two_buttons_panel.add(out_path_this_button);
        out_path_panel.add(out_path_label);
        out_path_panel.add(two_buttons_panel);
        co.add(out_path_panel);
        crack_mode.add(is_number);
        crack_mode.add(is_alphabet);
        crack_mode.add(is_alohabet_toUpperCase);
        crack_mode.add(is_symbol);
        crack_mode.add(key_start_number);
        crack_mode.add(bolang);
        crack_mode.add(key_end_number);
        co.add(crack_mode);
        co.add(start_crack);
        setBounds(600,300,600,600);
        setVisible(true);
        setTitle("Zip Crack");
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
    }
    private boolean read(String c,JTextArea jta,BufferedReader br) {
        while (true){
            try {
                if ((c = br.readLine()) == null) break;
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
            jta.append(c);
            System.out.println(c);
            if (c.equals("Invalid password")){
                return true;
            }
        }
        return false;
    }
    class ControlTheInput extends KeyAdapter {

        public void keyTyped(KeyEvent e) {
            String key="0123456789"+(char)8;
            if(key.indexOf(e.getKeyChar())<0){
                e.consume();//如果不是数字则取消
            }
        }
    }
    public static void main(String[] args) {
        new GUI();
    }
}


这个控制台的字符不一定是 utf-8 啊
试试看 ansi 以及 gb2312 等别的编码看看

  • 这篇文章:Java中BufferedWriter类的常用方法 也许有你想要的答案,你可以看看
  • 除此之外, 这篇博客: Java IO流之字符缓冲流(源码注释)【四】中的 1 BufferedWriter【字符缓冲输出流】 部分也许能够解决你的问题, 你可以仔细阅读以下内容或跳转源博客中阅读:
  • java.io.BufferedWriter是字符缓冲输出流,能够将文本写入字符输出流,缓冲各个字符,从而提供单个字符、数组和字符串的高效写入。

    可以指定缓冲区的大小,或者接受默认的大小(8192)。大多数情况下,默认值就足够了。

    字符缓冲输出流的基本方法与普通字符输出流一致,不再赘述,我们来看它们具备的特有方法。

    public void newLine() :
    写入一个行分隔符。行分隔符字符串由系统属性line.separator 定义,windows为 \r\n  linux为\n  mac为\r