Source:
public void extractDirectoryTables(CommandLine line, File pdfDirectory) throws ParseException { File[] pdfs = pdfDirectory.listFiles(new FilenameFilter() { public boolean accept(File dir, String name) { return name.endsWith(".pdf"); } }); for (File pdfFile : pdfs) { File outputFile = new File(getOutputFilename(pdfFile)); try{ extractFileInto(pdfFile, outputFile); } catch(ParseException pe){ System.out.println(pe.getMessage()); BufferedWriter loggingBufferedWriter = null; try { FileWriter loggingFileWriter = new FileWriter(pdfFile.getAbsolutePath().replaceFirst("[.][^.]+$", "") + "_LOG_FAIL" + ".txt", true); loggingBufferedWriter = new BufferedWriter(loggingFileWriter); loggingBufferedWriter.write("---------- START OF LOG ----------"); loggingBufferedWriter.newLine(); loggingBufferedWriter.write("FILE NAME: " + pdfFile.getName()); loggingBufferedWriter.newLine();loggingBufferedWriter.write("DATE PROCESSED: " + new SimpleDateFormat("MMMM dd, yyyy - hh:mma").format(Calendar.getInstance().getTime())); loggingBufferedWriter.newLine(); loggingBufferedWriter.write("ERROR - UNABLE TO PROCESS DOCUMENT"); loggingBufferedWriter.newLine(); } catch (IOException e) { throw new ParseException("Cannot create log file."); } finally { if (loggingBufferedWriter != null) { try { loggingBufferedWriter.write("---------- END OF LOG ----------"); loggingBufferedWriter.newLine(); loggingBufferedWriter.newLine(); loggingBufferedWriter.newLine(); loggingBufferedWriter.close(); } catch (IOException e) { System.out.println("Error in closing the logging BufferedWriter" + e); } } } } } }

Target:
public void extractDirectoryTables(CommandLine line, File pdfDirectory) throws ParseException { File[] pdfs = pdfDirectory.listFiles(new FilenameFilter() { public boolean accept(File dir, String name) { return name.endsWith(".pdf"); } }); for (File pdfFile : pdfs) { File outputFile = new File(getOutputFilename(pdfFile)); try{ extractFileInto(pdfFile, outputFile); } catch(ParseException pe){ BufferedWriter loggingBufferedWriter = null; try { FileWriter loggingFileWriter = new FileWriter(pdfFile.getAbsolutePath().replaceFirst("[.][^.]+$", "") + "_LOG_FAIL_" + new SimpleDateFormat("yyyy-MM-dd_hh-mmaa").format(Calendar.getInstance().getTime()) + ".txt", true); loggingBufferedWriter = new BufferedWriter(loggingFileWriter); loggingBufferedWriter.write("---------- START OF LOG ----------"); loggingBufferedWriter.newLine(); loggingBufferedWriter.write("FILE NAME: " + pdfFile.getName()); loggingBufferedWriter.newLine();loggingBufferedWriter.write("DATE PROCESSED: " + new SimpleDateFormat("MMMM dd, yyyy - hh:mma").format(Calendar.getInstance().getTime())); loggingBufferedWriter.newLine(); loggingBufferedWriter.write("ERROR - UNABLE TO PROCESS DOCUMENT"); loggingBufferedWriter.newLine(); Path logPassPath= Paths.get(pdfFile.getAbsolutePath().replaceFirst("[.][^.]+$", "") + "_LOG_PASS" + ".txt"); if(Files.exists(logPassPath)) { Files.delete(logPassPath); } } catch (IOException e) { throw new ParseException("Cannot create log file."); } finally { if (loggingBufferedWriter != null) { try { loggingBufferedWriter.write("---------- END OF LOG ----------"); loggingBufferedWriter.newLine(); loggingBufferedWriter.newLine(); loggingBufferedWriter.newLine(); loggingBufferedWriter.close(); } catch (IOException e) { } } } } } }