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

查看:230
本文介绍了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:


  • defualt打印队列是空的

  • 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: 注意:网址似乎已更改,并指向潜在的恶意软件)

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天全站免登陆