01: // IO.java
02: // $Id: IO.java,v 1.5 2000/08/16 21:37:58 ylafon Exp $
03: // (c) COPYRIGHT MIT and INRIA, 1998.
04: // Please first read the full copyright statement in file COPYRIGHT.html
05:
06: package org.w3c.util;
07:
08: import java.io.BufferedInputStream;
09: import java.io.BufferedOutputStream;
10: import java.io.File;
11: import java.io.FileInputStream;
12: import java.io.FileOutputStream;
13: import java.io.FilterOutputStream;
14: import java.io.IOException;
15: import java.io.InputStream;
16: import java.io.OutputStream;
17:
18: /**
19: * @version $Revision: 1.5 $
20: * @author Benoît Mahé (bmahe@w3.org)
21: */
22: public class IO {
23:
24: /**
25: * Copy source into dest.
26: */
27: public static void copy(File source, File dest) throws IOException {
28: BufferedInputStream in = new BufferedInputStream(
29: new FileInputStream(source));
30: BufferedOutputStream out = new BufferedOutputStream(
31: new FileOutputStream(dest));
32: byte buffer[] = new byte[1024];
33: int read = -1;
34: while ((read = in.read(buffer, 0, 1024)) != -1)
35: out.write(buffer, 0, read);
36: out.flush();
37: out.close();
38: in.close();
39: }
40:
41: /**
42: * Copy source into dest.
43: */
44: public static void copy(InputStream in, OutputStream out)
45: throws IOException {
46: BufferedInputStream bin = new BufferedInputStream(in);
47: BufferedOutputStream bout = new BufferedOutputStream(out);
48: byte buffer[] = new byte[1024];
49: int read = -1;
50: while ((read = bin.read(buffer, 0, 1024)) != -1)
51: bout.write(buffer, 0, read);
52: bout.flush();
53: bout.close();
54: bin.close();
55: }
56:
57: /**
58: * Delete recursivly
59: * @param file the file (or directory) to delete.
60: */
61: public static boolean delete(File file) {
62: if (file.exists()) {
63: if (file.isDirectory()) {
64: if (clean(file)) {
65: return file.delete();
66: } else {
67: return false;
68: }
69: } else {
70: return file.delete();
71: }
72: }
73: return true;
74: }
75:
76: /**
77: * Clean recursivly
78: * @param file the directory to clean
79: */
80: public static boolean clean(File file) {
81: if (file.isDirectory()) {
82: String filen[] = file.list();
83: for (int i = 0; i < filen.length; i++) {
84: File subfile = new File(file, filen[i]);
85: if ((subfile.isDirectory()) && (!clean(subfile))) {
86: return false;
87: } else if (!subfile.delete()) {
88: return false;
89: }
90: }
91: }
92: return true;
93: }
94:
95: }
|