Ich versuche, eine Textdatei mit dem angegebenen Code zu drucken. Alles funktioniert gut: Der Job wird in die Druckerwarteschlange des Druckers geladen. Nach einiger Zeit verschwindet es jedoch und nichts wird gedruckt. Außerdem gibt es keine Ausnahme oder Fehler im Code. Alles funktioniert gut, außer dass es nicht gedruckt wird. Ich verwende keine Druck-API.Drucken von Textdatei mit Java
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();
}
}
}
haben Sie versucht zu debuggen? –
was genau sollte ich debuggen? – Wijdan
Ich habe vergessen, ms eine Notiz zu erwähnen, auch geöffnet, nachdem ich die Drucktaste im Druckdialog gedrückt habe – Wijdan