KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > myoodb > core > FileHelper


1 ///////////////////////////////////////////////////////////////////////////////
2
//
3
// Copyright (C) 2003-@year@ by Thomas M. Hazel, MyOODB (www.myoodb.org)
4
//
5
// All Rights Reserved
6
//
7
// This program is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License and GNU Library
9
// General Public License as published by the Free Software Foundation;
10
// either version 2, or (at your option) any later version.
11
//
12
// This program is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License and GNU Library General Public License
16
// for more details.
17
//
18
// You should have received a copy of the GNU General Public License
19
// and GNU Library General Public License along with this program; if
20
// not, write to the Free Software Foundation, 675 Mass Ave, Cambridge,
21
// MA 02139, USA.
22
//
23
///////////////////////////////////////////////////////////////////////////////
24
package org.myoodb.core;
25
26 import java.io.*;
27
28 public final class FileHelper
29 {
30     private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(FileHelper.class);
31
32     public static boolean delete(File file)
33     {
34         if (file.delete() == false)
35         {
36             LOGGER.warn("Unable to delete(1): " + file);
37
38             System.gc();
39
40             if (file.delete() == false)
41             {
42                 LOGGER.warn("Unable to delete(2): " + file);
43
44                 try
45                 {
46                     Thread.sleep(100);
47                 }
48                 catch (InterruptedException JavaDoc e)
49                 {
50                 }
51
52                 return file.delete();
53             }
54         }
55
56         return true;
57     }
58
59     public static void delete(String JavaDoc name)
60     {
61         File file = new File(name);
62         if (file.exists() == true)
63         {
64             String JavaDoc[] files = file.list();
65
66             if ((files != null) && (files.length != 0))
67             {
68                 for (int i = 0; i < files.length; i++)
69                 {
70                     File subfile = new File(name, files[i]);
71
72                     if (subfile.isDirectory() == true)
73                     {
74                         delete(subfile.getPath());
75                     }
76                     else
77                     {
78                         if (delete(subfile) == false)
79                         {
80                             LOGGER.error("Unable to delete(3): " + subfile);
81                         }
82                     }
83                 }
84             }
85
86             delete(file);
87         }
88     }
89 }
90
Popular Tags