Caros boa tarde, estou com dificuldade num caso aparentemente simples mas n consigo resolver.
Tenho um código que faz copia de um arquivo em pdf inclui um carimbo nessa copia, transfere o arquivo original para uma outra pasta e por fim deleta o item original da pasta origem.
POREM NÃO ESTOU CONSEGUINDO DELETAR O ARQUIVO ORIGINAL DA PASTA ORIGEM, ja tentei de varias formas e não consigo fazer funcionar sem que um processo fique faltando, se eu consigo deletar ele não gera a copia carimbada, se ele gera a copia carimbada o não deleta ALGUEM PODE ME AJUDAR ?
segue o codigo abaixo
`public void copiarDoc(String caminhoarquivo, String caminhodestino) throws FileNotFoundException, IOException, DocumentException {
origem = new File(caminhoarquivo);
File destino = new File(caminhodestino);
FileInputStream fis = new FileInputStream(origem);
FileOutputStream fos = new FileOutputStream(destino);
FileChannel inChannel = fis.getChannel();
FileChannel outChannel = fos.getChannel();
long transferFrom = outChannel.transferFrom(inChannel, 0, inChannel.size());
fis.close();
fos.close();
inChannel.close();
outChannel.close();
}
public void aplicaCarimbo(String BN, String caminhoarquivo) throws DocumentException, IOException, RuntimeException {
PdfReader.unethicalreading = true;
//Cria o reader para o primeiro PDF
PdfReader reader = new PdfReader(caminhoarquivo);
Rectangle psize = reader.getPageSize(1);
float width = psize.getWidth();
float height = psize.getHeight();
reader = new PdfReader(caminhoarquivo);
Document document = new Document(new Rectangle(width, height));
PdfWriter writer = PdfWriter.getInstance(document,
new FileOutputStream(caminhoarquivo.substring(0, caminhoarquivo.length() - 4) + "-C.pdf"));
document.open();
int i = 0;
BN = BN.substring(0, BN.length() - 4);
while (i < reader.getNumberOfPages()) {
i++;
// Adiciona conteudo ao PDF Carimbado.
PdfContentByte cb = writer.getDirectContent();
// Thread.sleep(10);
document.newPage();
PdfContentByte under = writer.getDirectContentUnder();
PdfImportedPage page1 = writer.getImportedPage(reader, i);
cb.addTemplate(page1, 0, i * 0.2f);
//CARIMBO DA BN
BaseFont bf = BaseFont.createFont(BaseFont.COURIER_BOLD, BaseFont.CP1250, BaseFont.NOT_EMBEDDED);
cb.beginText();
cb.setFontAndSize(bf, 14);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " ________________", width / 6, 44, 0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " | |", width / 6, 32, 0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " | |", width / 6, 22, 0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " NR", width / 6, 28, 0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " " + BN, width / 6, 16, 0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " | |", width / 6, 12, 0);
cb.showTextAligned(PdfContentByte.ALIGN_CENTER, " ________________", width / 6, 14, 0);
cb.endText();
}
origem.delete();
document.close();
writer.close();
reader.close();
}`