KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > finalist > jag > util > FileUtils


1 /* Copyright (C) 2003 Finalist IT Group
2  *
3  * This file is part of JAG - the Java J2EE Application Generator
4  *
5  * JAG is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  * JAG is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with JAG; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  */

17
18 package com.finalist.jag.util;
19
20 import java.io.File JavaDoc;
21 import java.io.IOException JavaDoc;
22 import java.io.FileWriter JavaDoc;
23 import java.io.PrintWriter JavaDoc;
24
25 /**
26  * A couple of file-related utilities.
27  *
28  * @author Michael O'Connor - Finalist IT Group
29  */

30 public class FileUtils {
31
32    /**
33     * Just calling File.delete() only works sporadically on my system. It's a strange problem,
34     * but after a bit of trial and error, here's a solution that works for me..
35     *
36     * @param file the file to be deleted.
37     */

38    public static void deleteFile(File JavaDoc file) {
39       if (file.exists() && !file.delete()) {
40          System.gc();
41          if (!file.delete()) {
42             try {
43                Thread.sleep(100);
44             } catch (InterruptedException JavaDoc e) {
45             }
46             deleteFile(file);
47          }
48       }
49    }
50
51    /**
52     * Creates a file and writes the specified content into it.
53     *
54     * @param file
55     * @param content
56     * @throws IOException
57     */

58    public static void createFile(File JavaDoc file, String JavaDoc content) throws IOException JavaDoc {
59       file.getParentFile().mkdirs();
60       FileWriter JavaDoc writer = new FileWriter JavaDoc(file);
61       PrintWriter JavaDoc prwriter = new PrintWriter JavaDoc(writer);
62       prwriter.print(content);
63       prwriter.flush();
64       prwriter.close();
65       writer.close();
66    }
67
68 }
69
Popular Tags