程序报 java.io.IOException: 句柄无效。不知道为什么,请帮忙看看。
java.io.IOException: 句柄无效。
at java.io.FileOutputStream.writeBytes(Native Method)
at java.io.FileOutputStream.write(FileOutputStream.java:247)
at com.hippo.common.custsystem.Test$1.write(WindowsCmd.java:228)
at com.hippo.common.custsystem.Test$1.run(WindowsCmd.java:220)
at java.lang.Thread.run(Thread.java:595)
附上程序
class Test{
try {
FileOutputStream out = new FileOutputStream("F:\\temp2\\result.txt", true);
Test.writeSomething(out, "test ");
out.close();
} catch (FileNotFoundException ex) {
ex.printStackTrace();
} catch (IOException ex) {
/** @todo Handle this exception */
ex.printStackTrace();
}
}
public final static void writeSomething(final OutputStream output, final String line) throws
IOException {
Thread thread = new Thread(new Runnable(){
public void run() {
try {
this.write();
output.flush();
} catch (IOException ex) {
ex.printStackTrace();
}
}
public void write() throws IOException {
output.write(line.getBytes());
}
});
thread.start();
}//end of method
}
Test.writeSomething(out, "test ");
Thread.sleep(1000);
out.close();
应该就能解决问题了
建议你在关闭前要wait所有的线程都已经关闭。
不知道你的需求何在?