Java-检查文件是否在打印队列中/正在使用中
好的,我有一个程序:Java-检查文件是否在打印队列中/正在使用中,java,file,printing,locking,Java,File,Printing,Locking,好的,我有一个程序: 基于文件创建临时文件 用户输入 打印文件(可选) 删除文件(可选) 我的问题介于第2和第3阶段之间,我需要等待文件完成打印,直到我可以删除它 仅供参考:打印需要5-10分钟(在旧电脑上打印大文件) 因此,我需要从Java中检查: 解除打印队列为空 文件正在使用中(注意:打印时file.canWrite()返回true) 您是否检查了Java打印API?发件人: 公共接口 PrintJobListener 此侦听器的实现 接口应连接到 DocPrintJob用于监视的状态
- 解除打印队列为空
- 文件正在使用中(注意:打印时file.canWrite()返回true)
exampledepot.com/egs/javax.print/WaitForDone.html上还有一个相当完整的示例:
(注意:URL似乎已更改,并指向潜在的恶意软件)
谢谢,我基于此构建了解决方案&它似乎可以工作,当它不能工作时,将链接到代码。例如,Depot非常有用
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) {
}
}
}