打印机队列 java,Java - 检查文件是否在打印队列/正在使用中

OK I have a program that:

Creates a temporary file based on a

users input

Prints the File(Optional)

Deletes the File (Optional)

My problem sits between stages 2&3, I need to wait for the file to finish printing until I can delete it.

FYI: the printing will take 5-10 minutes (large file to spool on an old computer)

So I need to from Java be able to to check if:

the defualt print queue is empty

the file is in use (note: File.canWrite() returns true, when printing)

解决方案

public interface

PrintJobListener

Implementations of this listener

interface should be attached to a

DocPrintJob to monitor the status of

the printer job.

I imagine you can submit a print job and then monitor its status through this.

There's also a fairly complete example at exampledepot.com/egs/javax.print/WaitForDone.html: (Note: URL seems to have changed, and points to potential malware)

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) {

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值