0

I'm fairly new to programming and I've been trying to use PDFBox for a personal project that I have. I'm basically trying to verify if the PDF has specific keywords in it, if YES I want to transfer the file to a "approved" folder.

I know the code below is poor written, but I'm not able to transfer nor delete the file correctly:

   try (Stream<Path> filePathStream = Files.walk(Paths.get("C://pdfbox_teste"))) {

       filePathStream.forEach(filePath -> {

            if (Files.isRegularFile(filePath)) {

                String arquivo = filePath.toString();
                File file = new File(arquivo);

                try {
                    // Loading an existing document
                    PDDocument document = PDDocument.load(file);

                    // Instantiate PDFTextStripper class
                    PDFTextStripper pdfStripper = new PDFTextStripper();

                    String text = pdfStripper.getText(document);

                    String[] words = text.split("\\.|,|\\s");
                    for (String word : words) {

                        // System.out.println(word);

                        if (word.equals("Revisão") || word.equals("Desenvolvimento")) {


                    // System.out.println(word);
                            if(file.renameTo(new File("C://pdfbox_teste//Aprovados//" + file.getName()))){

                                document.close();
                                System.out.println("Arquivo transferido corretamente");
                                file.delete();

                            };

                        }

                    }
                    System.out.println("Fim do documento: " + arquivo);
                    System.out.println("----------------------------");
                    document.close();
                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }

            }
        });

I wanted to have the files transferred into the new folder. Instead, sometimes they only get deleted and sometimes nothing happens. I imagine the error is probably on the foreach, but I can't seem to find a way to fix it.

2 Answers2

1

You try to rename the file while it is still open, and only close it afterwards:

// your code, does not work
if(file.renameTo(new File("C://pdfbox_teste//Aprovados//" + file.getName()))){
    document.close();
    System.out.println("Arquivo transferido corretamente");
    file.delete();
};

Try to close the document first, so the file is no longer accessed by your process, and then it should be possible to rename it:

// fixed code:
document.close();
if(file.renameTo(new File("C://pdfbox_teste//Aprovados//" + file.getName()))){
    System.out.println("Arquivo transferido corretamente");
};

And as Mahesh K pointed out, you don't have to delete the (original) file after you renamed it. Rename does not make a duplicate where the original file would need to be deleted, it just renames it.

cello
  • 5,356
  • 3
  • 23
  • 28
0

After calling renameTo, you shouldn't be using delete.. as per my understanding renameTo works like move command. Pls see this

Mahesh K
  • 31
  • 5