gpt4 book ai didi

java - java中作业完成后返回主函数

转载 作者:太空宇宙 更新时间:2023-11-04 13:01:39 25 4
gpt4 key购买 nike

我这里有代码:对于主类:

 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 方法,而不等待打印作业完成。

问题是,如何在打印完成后才返回main方法并在打印完成后删除文件?我使用了system.exit(),但我不希望程序终止,但我只想在打印作业完成后返回到main方法来执行main方法中剩余的代码行。我希望有人能在这方面帮助我。

注意:我还查看了 return 和 system.exit() 之间的区别以及暂停。我还检查了为了删除该文件,我需要完整路径才能删除它,我发现我已经放置了完整路径。

最佳答案

为什么只删除作业中的文件?我认为在工作中添加删除代码会更好。或者,您可以在作业完成时向作业添加一个标志,并在 main 方法中添加等待代码(例如 while true)以等待标志为 true,然后执行删除。

关于java - java中作业完成后返回主函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34881062/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com