KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > nutch > fs > FileUtil


1 /* Copyright (c) 2003 The Nutch Organization. All rights reserved. */
2 /* Use subject to the conditions in http://www.nutch.org/LICENSE.txt. */
3
4 package net.nutch.fs;
5
6 import java.io.*;
7
8 /**
9  * A collection of file-processing util methods
10  */

11 public class FileUtil {
12     /**
13      * Delete a directory and all its contents. If
14      * we return false, the directory may be partially-deleted.
15      */

16     public static boolean fullyDelete(File dir) throws IOException {
17         return fullyDelete(new LocalFileSystem(), dir);
18     }
19     public static boolean fullyDelete(NutchFileSystem nfs, File dir) throws IOException {
20         // 20041022, xing.
21
// Currently nfs.detele(File) means fully delete for both
22
// LocalFileSystem.java and NDFSFileSystem.java. So we are okay now.
23
// If implementation changes in future, it should be modified too.
24
return nfs.delete(dir);
25     }
26
27     /**
28      * Copy a file's contents to a new location.
29      * Returns whether a target file was overwritten
30      */

31     public static boolean copyContents(NutchFileSystem nfs, File src, File dst, boolean overwrite) throws IOException {
32         if (nfs.exists(dst) && !overwrite) {
33             return false;
34         }
35
36         File dstParent = dst.getParentFile();
37         if (! nfs.exists(dstParent)) {
38             nfs.mkdirs(dstParent);
39         }
40
41         if (nfs.isFile(src)) {
42             DataInputStream in = new DataInputStream(nfs.open(src));
43             try {
44                 DataOutputStream out = new DataOutputStream(nfs.create(dst));
45                 byte buf[] = new byte[2048];
46                 try {
47                     int readBytes = in.read(buf);
48
49                     while (readBytes >= 0) {
50                         out.write(buf, 0, readBytes);
51                         readBytes = in.read(buf);
52                     }
53                 } finally {
54                     out.close();
55                 }
56             } finally {
57                 in.close();
58             }
59         } else {
60             nfs.mkdirs(dst);
61             File contents[] = nfs.listFiles(src);
62             if (contents != null) {
63                 for (int i = 0; i < contents.length; i++) {
64                     File newDst = new File(dst, contents[i].getName());
65                     if (! copyContents(nfs, contents[i], newDst, overwrite)) {
66                         return false;
67                     }
68                 }
69             }
70         }
71         return true;
72     }
73
74     /**
75      * Copy a file and/or directory and all its contents (whether
76      * data or other files/dirs)
77      */

78     public static void recursiveCopy(NutchFileSystem nfs, File src, File dst) throws IOException {
79         //
80
// Resolve the real target.
81
//
82
if (nfs.exists(dst) && nfs.isDirectory(dst)) {
83             dst = new File(dst, src.getName());
84         } else if (nfs.exists(dst)) {
85             throw new IOException("Destination " + dst + " already exists");
86         }
87
88         //
89
// Copy the items
90
//
91
if (! nfs.isDirectory(src)) {
92             //
93
// If the source is a file, then just copy the contents
94
//
95
copyContents(nfs, src, dst, true);
96         } else {
97             //
98
// If the source is a dir, then we need to copy all the subfiles.
99
//
100
nfs.mkdirs(dst);
101             File contents[] = nfs.listFiles(src);
102             for (int i = 0; i < contents.length; i++) {
103                 recursiveCopy(nfs, contents[i], new File(dst, contents[i].getName()));
104             }
105         }
106     }
107 }
108
Popular Tags