这个问题之前在 Stack Overflow 上被问过一次,但是堆栈跟踪与我的不同,我认为它不是由同一件事引起的。
这是我的堆栈跟踪:
java.io.IOException: Read error
at java.io.FileInputStream.readBytes(Native Method)
at java.io.FileInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at General.App.cmdString(App.java:393)
at General.App.cpauString(App.java:422)
at Functionality.RegistryScanThread.USBDScan(RegistryScanThread.java:109)
at Functionality.RegistryScanThread.doInBackground(RegistryScanThread.java:51)
at Functionality.RegistryScanThread.doInBackground(RegistryScanThread.java:1)
at javax.swing.SwingWorker$1.call(Unknown Source)
at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
at java.util.concurrent.FutureTask.run(Unknown Source)
at javax.swing.SwingWorker.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
这是我编写的更复杂程序的简化版本:
import java.util.*;
import java.io.*;
public class Test {
//global vars
public Test(){
// initializeing stuffs
}
public static void main(String[] args){
ArrayList<String> str = cmdString("reg query hklm");
for(String s : str){
System.out.println(s);
}
}
public static ArrayList<String> cmdString(String command){
boolean success = false;
ArrayList<String> result = new ArrayList<String>();
String line = "";
try{
Process p = Runtime.getRuntime().exec(command);
BufferedReader input = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((line = input.readLine()) != null) {result.add(new String(line));}
BufferedReader input2 = new BufferedReader(new InputStreamReader(p.getErrorStream()));
while ((line = input2.readLine()) != null) {result.add(new String(line));}
int exitvalue = p.waitFor();
if (exitvalue != 0){
System.out.println("error!!");
success = false;
}
else{
success = true;
}
for(String s : result){
System.out.println(s);
}
input.close();
input2.close();
return result;
} catch(Exception e) {
e.printStackTrace(System.err);
System.out.println("error!!!");
return result;
}
}
}
基本上。在上面的示例中,错误并不真正可见。但是在我的程序中,它在我第一次运行时正确执行了所有内容。
然后......当我第二次或第三次运行它时,就会发生错误。另外......似乎当我等待一段时间后再运行它时,似乎没有发生错误。
我将如何解决这个问题?