我正在尝试使用给定的代码打印文本文件。一切都很顺利:作业进入打印机的打印队列。然而一段时间后,它消失并且没有打印任何内容。此外,代码中没有异常或错误。一切都很顺利,除了不打印。我没有使用打印 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();
}
}
}
最佳答案
检查是否
所有导入都是:
导入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;
确保文件路径有效
您当前的打印机已设置为系统的默认打印机以及它是否正常工作
关于java - 用Java打印文本文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40674355/