View Javadoc
1 /***
2 * MyFile.java
3 *
4 * Project: Dependency Tool
5 *
6 * WHEN WHO WHAT
7 * 06.06.2003 pko initial public release
8 * 14.01.2003 pko creation
9 *
10 * Copyright 2003 ELCA Informatique SA
11 * Av. de la Harpe 22-24, 1000 Lausanne 13, Switzerland
12 * www.elca.ch
13 *
14 * This library is free software; you can redistribute it and/or
15 * modify it under the terms of the GNU Lesser General Public License
16 * as published by the Free Software Foundation; either version 2.1 of
17 * the License, or (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public
25 * License along with this library; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
27 * USA
28 */
29
30 package ch.elca.dependency.util;
31
32 import java.io.File;
33 import java.io.IOException;
34
35 /***
36 * Class <code>MyFile</code> used for a temporary directory which
37 * needs to be deleted on exit. The <code>delete()</code> method works
38 * recursively in case an instance of MyFile is a directory.
39 *
40 * @author Pawel Kowalski
41 * @version 1.0-beta
42 */
43 public class MyFile extends File {
44
45 /***
46 * Creates a new <code>MyFile</code> instance.
47 *
48 * @param pathname a <code>String</code> value
49 */
50 public MyFile(String pathname) {
51 super(pathname);
52 }
53
54 /***
55 * Creates a new <code>MyFile</code> instance.
56 *
57 * @param parent a <code>File</code> value
58 * @param child a <code>String</code> value
59 */
60 public MyFile(File parent, String child) {
61 super(parent, child);
62 }
63
64 /***
65 * Deletes the file or directory denoted by this abstract
66 * pathname. If this pathname denotes a directory, then the
67 * contents of this directory will be deleted recursively.
68 *
69 * @return a <code>boolean</code> value
70 */
71 public boolean delete() {
72 if ( isDirectory() ) {
73 String[] children = list();
74 for (int i=0; i < children.length; i++) {
75 boolean success = new MyFile(this, children[i]).delete();
76 if (!success) {
77 return false;
78 }
79 }
80 }
81
82 // The directory is now empty so delete it
83 //
84 return super.delete();
85 }
86
87 /***
88 * Atomically creates a new, empty file named by this abstract
89 * pathname if and only if a file with this name does not yet
90 * exist. All necessary parent directories will also be created.
91 *
92 * @return a <code>boolean</code> value
93 * @exception IOException if an error occurs
94 */
95 public boolean createNewFile() throws IOException {
96
97 // create necessary parent directories
98 //
99 File parentFile = getParentFile();
100 if (parentFile != null) {
101 if (!parentFile.isDirectory() && !parentFile.mkdirs()) {
102 throw new IOException("Could not create necessary parent directories");
103 }
104 }
105
106 // create the file
107 //
108 return super.createNewFile();
109 }
110 }
This page was automatically generated by Maven