You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TempFile.java 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.util;
  16. import java.io.File;
  17. import java.io.IOException;
  18. /**
  19. * Interface for creating temporary files. Collects them all into one directory by default.
  20. */
  21. public final class TempFile {
  22. /** The strategy used by {@link #createTempFile(String, String)} to create the temporary files. */
  23. private static TempFileCreationStrategy strategy = new DefaultTempFileCreationStrategy();
  24. /** Define a constant for this property as it is sometimes mistypes as "tempdir" otherwise */
  25. public static final String JAVA_IO_TMPDIR = "java.io.tmpdir";
  26. private TempFile() {
  27. // no instances of this class
  28. }
  29. /**
  30. * Configures the strategy used by {@link #createTempFile(String, String)} to create the temporary files.
  31. *
  32. * @param strategy The new strategy to be used to create the temporary files.
  33. *
  34. * @throws IllegalArgumentException When the given strategy is <code>null</code>.
  35. */
  36. public static void setTempFileCreationStrategy(TempFileCreationStrategy strategy) {
  37. if (strategy == null) {
  38. throw new IllegalArgumentException("strategy == null");
  39. }
  40. TempFile.strategy = strategy;
  41. }
  42. /**
  43. * Creates a new and empty temporary file. By default, files are collected into one directory and are
  44. * deleted on exit from the VM, although they can be kept by defining the system property
  45. * <code>poi.keep.tmp.files</code> (see {@link DefaultTempFileCreationStrategy}).
  46. * <p>
  47. * Don't forget to close all files or it might not be possible to delete them.
  48. *
  49. * @param prefix The prefix to be used to generate the name of the temporary file.
  50. * @param suffix The suffix to be used to generate the name of the temporary file.
  51. *
  52. * @return The path to the newly created and empty temporary file.
  53. *
  54. * @throws IOException If no temporary file could be created.
  55. */
  56. public static File createTempFile(String prefix, String suffix) throws IOException {
  57. return strategy.createTempFile(prefix, suffix);
  58. }
  59. public static File createTempDirectory(String name) throws IOException {
  60. return strategy.createTempDirectory(name);
  61. }
  62. }