1 28 29 30 package org.objectweb.ecm.taskdefs; 31 32 35 public class FileHelper 36 { 37 static public java.io.File [] 38 listFiles(java.io.File dir) 39 { 40 java.util.ArrayList files = new java.util.ArrayList (); 41 java.io.File [] childs = dir.listFiles(); 42 for (int i=0;i<childs.length;i++) { 43 if (childs[i].isDirectory()) { 44 files.addAll(java.util.Arrays.asList(listFiles(childs[i]))); 45 } 46 else { 47 files.add(childs[i]); 48 } 49 } 50 51 return (java.io.File [])files.toArray(new java.io.File [0]); 52 } 53 54 static public void 55 deleteDir(java.io.File dir) 56 { 57 if (dir==null) { 59 return ; 60 } 61 62 java.io.File [] files = dir.listFiles(); 63 if (files==null) { 64 files = new java.io.File [0]; 65 } 66 67 for (int i=0;i<files.length;i++) { 68 if (files[i].isDirectory()) { 69 deleteDir(files[i]); 70 } 71 else { 72 files[i].delete(); 73 } 74 } 75 76 dir.delete(); 77 } 78 79 static public void 80 moveFiles(java.io.File [] files, java.io.File destdir) 81 throws org.apache.tools.ant.BuildException 82 { 83 if (destdir==null) { 85 return ; 86 } 87 88 java.io.File destfile = null; 89 destdir.mkdirs(); 91 92 for (int i=0;i<files.length;i++) { 93 if (files[i].isDirectory()) { 95 java.io.File ndestdir = new java.io.File (destdir, files[i].getName()); 96 moveFiles(files[i].listFiles(), ndestdir); 97 continue; 98 } 99 100 destfile = new java.io.File (destdir, files[i].getName()); 101 if ((destfile.exists()) && (destfile.lastModified()>files[i].lastModified())) { 104 continue; 105 } 106 107 java.io.FileInputStream in = null; 109 java.io.FileOutputStream out = null; 110 try { 111 if (!destfile.exists()) { 113 destfile.createNewFile(); 114 } 115 116 in = new java.io.FileInputStream (files[i]); 117 out = new java.io.FileOutputStream (destfile); 118 119 byte[] buffer = new byte[8*1024]; 120 int count = 0; 121 do { 122 out.write(buffer, 0, count); 123 count = in.read(buffer, 0, buffer.length); 124 } while (count!=-1); 125 126 } catch (Exception ex) { 127 String msg = "exception caught: "+ex.getMessage(); 128 ex.printStackTrace(); 129 throw new org.apache.tools.ant.BuildException(msg); 130 } finally { 131 if (in!=null) { try {in.close();} catch(java.io.IOException ex) {} } 132 if (out!=null) { try {out.close();} catch(java.io.IOException ex) {} } 133 } 134 } 135 } 136 137 static public java.io.File 138 createTempDir() 139 throws org.apache.tools.ant.BuildException 140 { 141 java.io.File tmpfile = null; 143 try { 144 tmpfile = java.io.File.createTempFile("temp", null); 145 } catch (Exception ex) { 146 throw new org.apache.tools.ant.BuildException(ex.getMessage()); 147 } 148 149 String path = tmpfile.getPath(); 150 tmpfile.delete(); 151 152 java.io.File tmpdir = null; 154 try { 155 tmpdir = new java.io.File (path); 156 tmpdir.mkdirs(); 157 } catch (Exception ex) { 158 throw new org.apache.tools.ant.BuildException(ex.getMessage()); 159 } 160 161 return tmpdir; 162 } 163 } 164 | Popular Tags |