簡體   English   中英

用Java完成作業后返回主函數

[英]Returning to the main function after job completion in java

我在這里有代碼:對於主類:

 PrintingOperation printnew = new PrintingOperation();
 printnew.PrintingOperation(newfile);

 System.out.println("The file is to be deleted and will return from the start");

 File toDelete = listOfFiles[0]; //This output the full path of the file to be deleted
 System.out.println(toDelete); //I confirmed that it is a full path
 toDelete.delete(); //The problem occurs here -> It does not delete the file
 System.out.println("The file is deleted");

對於打印服務:

   public String PrintingOperation(String file_name){

    System.out.println("The file name is: " + file_name);
    PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
    PdfDecoder decodePdf = new PdfDecoder(true);
    try {
    decodePdf.openPdfFile(file_name);
    FontMappings.setFontReplacements();

 } catch (Exception e) {
    //...
 }

//Setting the attributes
    PrintRequestAttributeSet attributeSet = new HashPrintRequestAttributeSet();
    JobName jobName = new JobName(file_name, null);
    attributeSet.add(jobName);
    attributeSet.add(new Copies(1)); //NUMBER OF COPIES
    attributeSet.add(MediaSizeName.ISO_A4); // Paper size : ISO_A4 or NA_LEGAL
    attributeSet.add(Chromaticity.COLOR); //COLOR or MONOCHROME
    decodePdf.setPrintAutoRotateAndCenter(false);

    PrintService[] services = PrintServiceLookup.lookupPrintServices(DocFlavor.SERVICE_FORMATTED.PAGEABLE, attributeSet);
    for(PrintService s : services) {
      System.out.println(s.getName());
    }
        PrintService printingDevice = null; 
    for(PrintService s : services) {
      if(s.getName().equals("Brother MFC-5890CN Printer")) { //Change it to the printer available
           printingDevice = s; 
      }
    }
    PdfBook pdfBook = new PdfBook(decodePdf, printingDevice, attributeSet);
    SimpleDoc doc = new SimpleDoc(pdfBook, DocFlavor.SERVICE_FORMATTED.PAGEABLE, null);
    DocPrintJob printJob = printingDevice.createPrintJob();
    JobCompleteMonitor monitor = new JobCompleteMonitor();
    printJob.addPrintJobListener(monitor);
    try {
      printJob.print(doc, attributeSet);
    } catch (PrintException e) {
        //...
    }
    monitor.waitForJobCompletion();


    System.out.println("Exiting printing process");      
    return null;
}
private static class JobCompleteMonitor extends PrintJobAdapter {

    private boolean completed = false;

    @Override
    public void printJobCanceled(PrintJobEvent pje) {
        signalCompletion();
    }

    @Override
    public void printJobCompleted(PrintJobEvent pje) {
        signalCompletion();
    }

    @Override
    public void printJobFailed(PrintJobEvent pje) {
        signalCompletion();
    }

    @Override
    public void printJobNoMoreEvents(PrintJobEvent pje) {
        signalCompletion();
    }

    private void signalCompletion() {

synchronized (JobCompleteMonitor.this) {

        completed = true;

        JobCompleteMonitor.this.notify();
}

  }

public synchronized void waitForJobCompletion() {

  try {

while (!completed) {

    wait();

}

  } catch (InterruptedException e) {

 }


}
}

一旦程序到達

printJob.print(doc,attributeSet)

打印機將開始其操作並打印文檔。 但是,它會自動返回到main方法,並且不會刪除正在打印的文件。 我放置了等待打印作業完成的功能,但它仍返回主方法,而無需等待打印作業完成。

問題是 ,如何僅在打印過程完成后返回到main方法,並在打印過程后刪除文件? 我使用了system.exit(),但是我不希望程序終止,但是我只想在打印作業完成后返回main方法,以執行main方法中的其余代碼行。 我希望有人可以幫助我。

注意:我還查看了return和system.exit()之間的區別以及暫停。 我還檢查了一下,為了刪除文件,我需要完整的路徑才能刪除它,並且發現我已經放置了完整的路徑。

為什么只刪除作業中的文件? 我認為向工作中添加刪除代碼會更好。 或者,您可以在作業完成時向其添加一個標志,並在main方法中添加等待代碼(例如,當為true時)以等待該標志為true,然后進行刪除。

我找到了最簡單的答案。 只是因為我沒有關閉pdfFile。

encodePdf.closePdfFile(); 返回之前應添加該文件,以便刪除該文件。

:)希望這會對其他人有所幫助:)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM