1 4 package uk.ac.roe.antigen.utils; 5 6 import java.io.File ; 7 import java.io.FileInputStream ; 8 import java.io.FileOutputStream ; 9 import java.io.IOException ; 10 import java.net.URI ; 11 import java.nio.channels.FileChannel ; 12 import java.util.logging.Logger ; 13 14 20 public class CopyableFile extends File { 21 24 private static final Logger logger = Logger.getLogger(CopyableFile.class.getName()); 25 26 public static void main(String [] args) throws IOException { 27 CopyableFile file = new CopyableFile("C:/cygwin"); 29 file.copyTo(new File ("C:/cygwin2")); 30 System.out.println("is abs "+file.isAbsolute()); 32 System.out.println("absolute file " + file.getAbsoluteFile()); 33 System.out.println("absolute path "+file.getAbsolutePath()); 34 System.out.println("canonical file "+file.getCanonicalFile()); 35 System.out.println("canonical path "+ file.getCanonicalPath()); 36 System.out.println("name "+file.getName()); 37 System.out.println("parent "+file.getParent()); 38 System.out.println("parent file "+file.getParentFile()); 39 System.out.println("path "+file.getPath()); 40 41 42 } 43 44 45 46 51 public void copyTo(File newLocation) throws IOException { 52 logger.fine("Copying "+getAbsolutePath()+" to "+newLocation); 53 if (isFile()) { 54 logger.fine("(file)"); 55 copyFileTo(newLocation); 56 } else { 57 logger.fine("(folder)"); 58 File [] files = listFiles(); 59 logger.fine("Contains "+files.length+" files"); 60 newLocation.mkdirs(); 61 for (int i = 0;i<files.length;++i) { 62 logger.fine("Processing " +files[i]); 63 CopyableFile file = new CopyableFile(files[i]); 64 File newFile = new File (newLocation,file.getName()); 65 file.copyTo(newFile); 66 } 67 } 68 69 } 70 71 75 public boolean recursivelyDelete() { 76 if (isFile()) { 77 logger.fine("deleting "+getName()); 78 return delete(); 79 } else { 80 File [] files = listFiles(); 81 for (int i = 0;i<files.length;++i) { 82 CopyableFile file = new CopyableFile(files[i]); 83 file.recursivelyDelete(); 84 } 85 return delete(); 86 } 87 88 } 89 90 private void copyFileTo(File destination) throws IOException { 91 logger.fine("Copying from "+destination+"..."); 92 FileChannel srcChannel = new FileInputStream (getAbsolutePath()).getChannel(); 93 logger.fine("...got source channel "+srcChannel+"..."); 94 FileChannel destChannel = new FileOutputStream (new File (destination.getAbsolutePath())).getChannel(); 95 logger.fine("...got destination channel "+destChannel+"..."); 96 logger.fine("...Got channels..."); 97 destChannel.transferFrom(srcChannel, 0, srcChannel.size()); 98 logger.fine("...transferred."); 99 srcChannel.close(); 100 destChannel.close(); 101 } 102 103 106 public CopyableFile(String arg0) { 107 super(arg0); 108 } 109 110 114 public CopyableFile(File arg0, String arg1) { 115 super(arg0, arg1); 116 } 117 118 public CopyableFile(File arg0) { 119 super(arg0.getAbsolutePath()); 120 } 121 122 123 127 public CopyableFile(String arg0, String arg1) { 128 super(arg0, arg1); 129 } 130 133 public CopyableFile(URI arg0) { 134 super(arg0); 135 } 136 } 137 | Popular Tags |