java - 用Java打印文本文件

标签 java text printing

我正在尝试使用给定的代码打印文本文件。一切都很顺利:作业进入打印机的打印队列。然而一段时间后,它消失并且没有打印任何内容。此外,代码中没有异常或错误。一切都很顺利,除了不打印。我没有使用打印 API。

public class PipedPrint implements Runnable {
    PipedOutputStream pos = new PipedOutputStream();
    PipedInputStream pis = new PipedInputStream();
    byte[] abPrinterBytes = null;

    private void mainProcess() throws IOException, PrintException, InterruptedException {
        abPrinterBytes = getBytesFromFile();
        pis.connect(pos);
        Thread randWriter = new Thread(this);
        randWriter.start();
        PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
        DocFlavor flavor = DocFlavor.INPUT_STREAM.AUTOSENSE;
        PrintService printService[] = PrintServiceLookup.lookupPrintServices(flavor, pras);
        PrintService defaultService = PrintServiceLookup.lookupDefaultPrintService();
        PrintService service = ServiceUI.printDialog(null, 200, 200, printService, defaultService, flavor, pras);
        if (defaultService != null) {
            DocPrintJob job = defaultService.createPrintJob();
            DocAttributeSet das = new HashDocAttributeSet();
            Doc doc = new SimpleDoc(pis, flavor, das);
            System.out.println("Main::Fired Print");
            job.print(doc, pras);
            System.out.println("Main::Done Print");
        }
        randWriter.join();
        System.out.println("Main::Join Over");
    }

    private byte[] getBytesFromFile() throws IOException {
        File fFile = new File("C:\\Users\\Wijdan\\Documents\\NetBeansProjects\\demo_print\\src\\demo_print\\pass.txt"); //File to print
        byte[] abFileBytes = new byte[(int) fFile.length()];
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(fFile));
        for (int i = 0; i < abFileBytes.length; i += in .read(abFileBytes, i, (abFileBytes.length - i)));
        return abFileBytes;
    }

    public void run() {
        try {
            Thread.sleep(5000);
            System.out.println("RandWriter started Writing");
            for (int i = 0; i < abPrinterBytes.length; i += 400) {
                pos.write(abPrinterBytes, i, ((i + 400) < abPrinterBytes.length) ? 400 : (abPrinterBytes.length - i));
                System.out.println("Thread:: Wrote bytes. Sleeping....");
                Thread.sleep(500);
            }
            pos.close();
            System.out.println("Thread::Closed pos. Exitting Thread...");
        } catch (Exception ie) {
            ie.printStackTrace();
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        PipedPrint t1 = new PipedPrint();
        try {
            t1.mainProcess();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

最佳答案

检查是否

  1. 所有导入都是:

    导入java.io.BufferedInputStream; 导入java.io.File; 导入 java.io.FileInputStream; 导入java.io.IOException; 导入 java.io.PipedInputStream; 导入java.io.PipedOutputStream;

    导入javax.print.Doc; 导入 javax.print.DocFlavor; 导入 javax.print.DocPrintJob; 导入 javax.print.PrintException; 导入 javax.print.PrintService; 导入 javax.print.PrintServiceLookup; 导入 javax.print.ServiceUI; 导入 javax.print.SimpleDoc; 导入 javax.print.attribute.DocAttributeSet; 导入 javax.print.attribute.HashDocAttributeSet; 导入 javax.print.attribute.HashPrintRequestAttributeSet; 导入javax.print.attribute.PrintRequestAttributeSet;

  2. 确保文件路径有效

  3. 您当前的打印机已设置为系统的默认打印机以及它是否正常工作

关于java - 用Java打印文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40674355/

相关文章:

java - 是否有真实的 Java 示例定义何时使用一维数组而不是数组列表更好?

java - 在 Wildfly 上重新部署导致内存不足 : metaspace

python - Python 中通过随机化减少文本文件

swift text.draw 方法搞乱了 CGContext

java - 确保*所有*代码路径返回有效的 JDBC 连接或抛出异常(禁止 "null")

java - Tomcat 7.1 为来自同一客户端的每个请求创建新 session

python - 使用 python 提取包含关键字或短语列表的句子

java - 我编写了计算最大公约数的代码,但存在问题

java - 平均字长

java - 强制打印机进入错误状态