01: /* ====================================================================
02: Licensed to the Apache Software Foundation (ASF) under one or more
03: contributor license agreements. See the NOTICE file distributed with
04: this work for additional information regarding copyright ownership.
05: The ASF licenses this file to You under the Apache License, Version 2.0
06: (the "License"); you may not use this file except in compliance with
07: the License. You may obtain a copy of the License at
08:
09: http://www.apache.org/licenses/LICENSE-2.0
10:
11: Unless required by applicable law or agreed to in writing, software
12: distributed under the License is distributed on an "AS IS" BASIS,
13: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: See the License for the specific language governing permissions and
15: limitations under the License.
16: ==================================================================== */
17: package org.apache.poi.util;
18:
19: import java.io.File;
20: import java.io.IOException;
21: import java.util.Random;
22:
23: /**
24: * Interface for creating temporary files. Collects them all into one directory.
25: *
26: * @author Glen Stampoultzis
27: */
28: public class TempFile {
29: static File dir;
30: static Random rnd = new Random();
31:
32: /**
33: * Creates a temporary file. Files are collected into one directory and by default are
34: * deleted on exit from the VM. Files can be kept by defining the system property
35: * <code>poi.keep.tmp.files</code>.
36: * <p>
37: * Dont forget to close all files or it might not be possible to delete them.
38: */
39: public static File createTempFile(String prefix, String suffix)
40: throws IOException {
41: if (dir == null) {
42: dir = new File(System.getProperty("java.io.tmpdir"),
43: "poifiles");
44: dir.mkdir();
45: if (System.getProperty("poi.keep.tmp.files") == null)
46: dir.deleteOnExit();
47: }
48:
49: File newFile = new File(dir, prefix + rnd.nextInt() + suffix);
50: if (System.getProperty("poi.keep.tmp.files") == null)
51: newFile.deleteOnExit();
52: return newFile;
53: }
54:
55: }
|