1 2 3 4 package net.nutch.fs; 5 6 import java.io.*; 7 8 11 public class FileUtil { 12 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 return nfs.delete(dir); 25 } 26 27 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 78 public static void recursiveCopy(NutchFileSystem nfs, File src, File dst) throws IOException { 79 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 if (! nfs.isDirectory(src)) { 92 copyContents(nfs, src, dst, true); 96 } else { 97 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 |