KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > util > TempFile


1 /* ====================================================================
2    Copyright 2002-2004 Apache Software Foundation
3
4    Licensed under the Apache License, Version 2.0 (the "License");
5    you may not use this file except in compliance with the License.
6    You may obtain a copy of the License at
7
8        http://www.apache.org/licenses/LICENSE-2.0
9
10    Unless required by applicable law or agreed to in writing, software
11    distributed under the License is distributed on an "AS IS" BASIS,
12    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13    See the License for the specific language governing permissions and
14    limitations under the License.
15 ==================================================================== */

16 package org.apache.poi.util;
17
18 import java.io.File JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.util.Random JavaDoc;
21
22 /**
23  * Interface for creating temporary files. Collects them all into one directory.
24  *
25  * @author Glen Stampoultzis
26  */

27 public class TempFile
28 {
29     static File JavaDoc dir;
30     static Random JavaDoc rnd = new Random JavaDoc();
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 JavaDoc createTempFile(String JavaDoc prefix, String JavaDoc suffix) throws IOException JavaDoc
40     {
41         if (dir == null)
42         {
43             dir = new File JavaDoc(System.getProperty("java.io.tmpdir"), "poifiles");
44             dir.mkdir();
45             if (System.getProperty("poi.keep.tmp.files") == null)
46                 dir.deleteOnExit();
47         }
48
49         File JavaDoc newFile = new File JavaDoc(dir, prefix + rnd.nextInt() + suffix);
50         if (System.getProperty("poi.keep.tmp.files") == null)
51             newFile.deleteOnExit();
52         return newFile;
53     }
54
55
56
57 }
58
Popular Tags