Search code examples
javapdftemporary-filestemporaryicepdf

How to remove ICEpdf temporary files after application has finished working?


I use ICEpdf library for PDF displaying at my desktop java application. Application adds annotations to PDF at runtime, but without changing original files — changes are displayed only during one 'session'. I recently discovered that application creates a lot of temporary files which consume quite a lot of disk space.

Method org.icepdf.core.pobjects.Document.setInputStream has the following code:

// Delete temp file on exit 
tempFile.deleteOnExit();

So I suppose it has to remove temporary files after it used them, but it does not:

ICE pdf temporary files

How can I programmatically remove all files created by application on exit or make standard file removing work?


Solution

  • To get temp folder path:

    FileSystems.getDefault().getPath(System.getProperty("java.io.tmpdir"))
    

    To remove files:

    try (DirectoryStream<Path> paths = Files.newDirectoryStream(pathToDir, regex)){
            paths.forEach(path -> path.toFile().delete());
        } catch (IOException e) {
            // handle io exception
        }
    

    where regex is filename pattern. In your case: "IcePdf*"