1 6 7 package SOFA.Connector.EEG.fileutil; 8 9 14 public class Copy { 15 16 17 private Copy() { 18 } 19 20 public static void copy(java.io.File destination, java.io.File source) throws CopyException { 21 if (source.isDirectory()) { 22 if (!destination.isDirectory()) { 23 throw new CopyException("Destination '"+destination.getName()+"' is not directory."); 24 } 25 copyDirectory(destination,source); 26 } else { 27 if (destination.isDirectory()) { 28 destination=new java.io.File (destination,source.getName()); 29 } 30 copyFile(destination,source); 31 } 32 } 33 34 protected static void copyDirectory(java.io.File destination, java.io.File source) throws CopyException { 35 java.io.File [] list=source.listFiles(); 36 for (int i=0;i<list.length;i++) { 37 java.io.File dest=new java.io.File (destination,list[i].getName()); 38 if (list[i].isDirectory()) { 39 dest.mkdir(); 40 copyDirectory(dest,list[i]); 41 } else { 42 copyFile(dest,list[i]); 43 } 44 } 45 } 46 47 protected static void copyFile(java.io.File destination, java.io.File source) throws CopyException { 48 try { 49 java.io.FileInputStream inStream=new java.io.FileInputStream (source); 50 java.io.FileOutputStream outStream=new java.io.FileOutputStream (destination); 51 52 int len; 53 byte[] buf=new byte[2048]; 54 55 while ((len=inStream.read(buf))!=-1) { 56 outStream.write(buf,0,len); 57 } 58 } catch (Exception e) { 59 throw new CopyException("Can't copy file "+source+" -> "+destination+".",e); 60 } 61 } 62 } 63 | Popular Tags |