Control temporary files

On this page

GroupDocs.Viewer allows you to control a way temporary files and directories is created. Follow these steps to implement custom logic for creating temporary files and folders:

  1. Create a class that implements the com.groupdocs.viewer.common.tempfiles.TemporaryFileManager interface and its methods:
class CustomTemporaryFileManager implements TemporaryFileManager {

    @Override
    public Path createTempDirectory() {
        // Implementation
    }

    @Override
    public Path createTempDirectory(String... pathsAsStrings) {
        // Implementation
    }

    @Override
    public Path createTempDirectory(Path... paths) {
        // Implementation
    }

    @Override
    public Path createTempFile() {
        // Implementation
    }

    @Override
    public Path createTempFile(String... pathsAsStrings) {
        // Implementation
    }

    @Override
    public Path createTempFile(Path... paths) {
        // Implementation
    }

    @Override
    public void delete(Path path) {
        // Implementation
    }

    @Override
    public void cleanup() {
        // Implementation
    }
}
  1. Create an object of the class and specify it as a parameter of the setInstance method of TemporaryFileManagerFactory:

    final CustomTemporaryFileManager customTemporaryFileManager = new CustomTemporaryFileManager();
    TemporaryFileManagerFactory.setInstance(customTemporaryFileManager);
    
    Now GroupDocs.Viewer uses this implementation for creating temporary files and directories.

  2. Use the implementation for your needs as follows:

final TemporaryFileManager temporaryFileManager = TemporaryFileManagerFactory.getInstance();
final Path tempFile = temporaryFileManager.createTempFile("temp.txt");
// Do something with the file
temporaryFileManager.delete(tempFile);
Note
Default implementation creates the temporary files and directories in a directory specified by the java.io.tmpdir property.

On this page