好的我有一个程序:
我的问题在第2阶段和第3阶段之间,我需要等待文件完成打印,直到我可以删除它。
仅供参考:打印需要5-10分钟(大型文件在旧计算机上假脱机)
所以我需要从Java能够检查是否:
defualt打印队列为空
文件正在使用中(注意:File.canWrite()在打印时返回true)
答案 0 :(得分:5)
您检查过Java Print API吗?来自http://download.oracle.com/javase/1.4.2/docs/api/javax/print/event/PrintJobListener.html:
公共界面
PrintJobListener
此侦听器的实现 接口应附加到 DocPrintJob监控状态 打印机工作。
我想你可以提交一份打印作业,然后通过它来监控它的状态。
在 (注意:URL似乎已更改,并指向潜在的恶意软件)中还有一个相当完整的示例exampledepot.com/egs/javax.print/WaitForDone.html:
try {
// Open the image file
InputStream is = new BufferedInputStream(
new FileInputStream("filename.gif"));
// Create the print job
DocPrintJob job = service.createPrintJob();
Doc doc = new SimpleDoc(is, flavor, null);
// Monitor print job events
PrintJobWatcher pjDone = new PrintJobWatcher(job);
// Print it
job.print(doc, null);
// Wait for the print job to be done
pjDone.waitForDone();
// It is now safe to close the input stream
is.close();
} catch (PrintException e) {
} catch (IOException e) {
}
class PrintJobWatcher {
// true iff it is safe to close the print job's input stream
boolean done = false;
PrintJobWatcher(DocPrintJob job) {
// Add a listener to the print job
job.addPrintJobListener(new PrintJobAdapter() {
public void printJobCanceled(PrintJobEvent pje) {
allDone();
}
public void printJobCompleted(PrintJobEvent pje) {
allDone();
}
public void printJobFailed(PrintJobEvent pje) {
allDone();
}
public void printJobNoMoreEvents(PrintJobEvent pje) {
allDone();
}
void allDone() {
synchronized (PrintJobWatcher.this) {
done = true;
PrintJobWatcher.this.notify();
}
}
});
}
public synchronized void waitForDone() {
try {
while (!done) {
wait();
}
} catch (InterruptedException e) {
}
}
}