废话不多说,直接上代码
ProcessBuilder builder = new ProcessBuilder("cmd.exe", "/c", "chcp 65001");
builder.redirectErrorStream(true);
try {
Process process = builder.start();
BufferedReader br = null;
BufferedWriter bw = null;
try {
bw = new BufferedWriter(new OutputStreamWriter(process.getOutputStream(),StandardCharsets.UTF_8));
br = new BufferedReader(new InputStreamReader(process.getInputStream(),StandardCharsets.UTF_8));
String s;
bw.write("bz.exe x -o:"+out_path_label.getText()+" "+file_path_label.getText()+"\n");
bw.flush();
while ((s = br.readLine()) != null){
System.out.println(s);
jta.append(s+"\n");
}
KeyDictionary kd = new KeyDictionary();
// 进行CMD命令操作之前的代码,
// 将对输出流BufferedWriter bw 的引用传递给后续的代码
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()+"\n";
try {
bw.write(key);
bw.flush();
String c = null;
if (!read(c,jta,br)){
break;
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
if (key.equals(end_key)){
break;
}
}
}
} catch (IOException ex) {
throw new RuntimeException(ex);
} finally {
if (bw != null) {
try {
bw.flush();
bw.close();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
if (br != null) {
try {
br.close();
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}
} catch (IOException ex) {
throw new RuntimeException(ex);
}
报错如下:
"C:\Program Files\Java\jdk-17\bin\java.exe" "-javaagent:F:\java\idea\IntelliJ IDEA Community Edition 2022.3.2\lib\idea_rt.jar=12516:F:\java\idea\IntelliJ IDEA Community Edition 2022.3.2\bin" -Dfile.encoding=UTF-8 -classpath "F:\java\projects\Zip Crack\out\production\Zip Crack" GUI
Active code page: 65001
Exception in thread "Thread-0" java.lang.RuntimeException: java.io.IOException: 管道正在被关闭。
at GUI$1$1.run(GUI.java:96)
at java.base/java.lang.Thread.run(Thread.java:833)
Caused by: java.io.IOException: 管道正在被关闭。
at java.base/java.io.FileOutputStream.writeBytes(Native Method)
at java.base/java.io.FileOutputStream.write(FileOutputStream.java:349)
at java.base/java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:81)
at java.base/java.io.BufferedOutputStream.flush(BufferedOutputStream.java:142)
at java.base/sun.nio.cs.StreamEncoder.implFlush(StreamEncoder.java:320)
at java.base/sun.nio.cs.StreamEncoder.flush(StreamEncoder.java:160)
at java.base/java.io.OutputStreamWriter.flush(OutputStreamWriter.java:248)
at java.base/java.io.BufferedWriter.flush(BufferedWriter.java:257)
at GUI$1$1.run(GUI.java:93)
... 1 more