KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > util > TempFileStore


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.core.util;
17
18 import java.io.File JavaDoc;
19
20 import org.columba.core.config.DefaultConfigDirectory;
21 import org.columba.core.io.DiskIO;
22
23 /**
24  * Factory to create temporary Files on the file storage.
25  */

26 public final class TempFileStore {
27     private static File JavaDoc tempDir;
28
29     static {
30         File JavaDoc configDir = DefaultConfigDirectory.getInstance().getCurrentPath();
31
32         tempDir = new File JavaDoc(configDir, "tmp");
33         DiskIO.emptyDirectory(tempDir);
34         DiskIO.ensureDirectory(tempDir);
35     }
36
37     /**
38      * Utility class should not have a public constructor.
39      */

40     private TempFileStore() {
41     }
42
43     /**
44      * Returns a String with spaces replaced by underscore.
45      * @param s in string
46      * @return incoming string without spaces.
47      */

48     private static String JavaDoc replaceWhiteSpaces(String JavaDoc s) {
49         return s.replace(' ', '_');
50     }
51
52     /**
53      * Create a temporary file on the temporary folder storage.
54      * @return a File. File suffix is tmp.
55      */

56     public static File JavaDoc createTempFile() {
57         return createTempFileWithSuffix("tmp");
58     }
59
60     /**
61      * Create a temporary file with the specified filename.
62      * @param name the name of the file.
63      * @return a File.
64      */

65     public static File JavaDoc createTempFile(String JavaDoc name) {
66         return createTemporaryFile(replaceWhiteSpaces(name));
67     }
68
69     /**
70      * Create a temporary file with the specified file name suffix.
71      * @param suffix the suffix for the file.
72      * @return a File.
73      */

74     public static File JavaDoc createTempFileWithSuffix(String JavaDoc suffix) {
75         return createTemporaryFile("columba" + System.currentTimeMillis() + "." + suffix);
76     }
77
78     /**
79      * Creates a temporary file that is removed when the program exits.
80      * @param name the name of the file.
81      * @return a File.
82      */

83     private static File JavaDoc createTemporaryFile(String JavaDoc name) {
84         File JavaDoc newFile = new File JavaDoc(tempDir, name);
85         newFile.deleteOnExit();
86         return newFile;
87     }
88 }
89
Popular Tags