Java - 检查文件是否在打印队列中/正在使用中 [英] Java -Check if file is in print Queue / In Use

查看:12
本文介绍了Java - 检查文件是否在打印队列中/正在使用中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我有一个程序:

  1. 根据一个临时文件创建一个用户输入
  2. 打印文件(可选)
  3. 删除文件(可选)

我的问题位于阶段 2 和 3 之间,我需要等待文件完成打印,直到我可以将其删除.

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

仅供参考:打印需要 5-10 分钟(在旧计算机上后台处理大文件)

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

所以我需要从 Java 中检查是否:

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

  • 默认打印队列为空

  • the defualt print queue is empty

文件正在使用中(注意:File.canWrite() 返回true,打印时)

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

推荐答案

您是否检查过 Java Print API?来自 http://download.oracle.com/javase/1.4.2/docs/api/javax/print/event/PrintJobListener.html:

Have you checked the Java Print API? From http://download.oracle.com/javase/1.4.2/docs/api/javax/print/event/PrintJobListener.html:

公共接口

PrintJobListener

PrintJobListener

这个监听器的实现接口应附加到DocPrintJob 监控状态打印机作业.

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.

exampledepot.com/egs/javax.print/WaitForDone.html 上还有一个相当完整的例子:(注意:URL 似乎是已更改,并指向潜在的恶意软件)

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

这篇关于Java - 检查文件是否在打印队列中/正在使用中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆