1 26 27 28 package net.sourceforge.groboutils.util.io.v1; 29 30 import java.util.Hashtable ; 31 import java.util.Vector ; 32 import java.util.Stack ; 33 34 import java.io.IOException ; 35 import java.io.FileNotFoundException ; 36 import java.io.File ; 37 import java.io.FilenameFilter ; 38 import java.io.BufferedOutputStream ; 39 import java.io.BufferedInputStream ; 40 import java.io.FileOutputStream ; 41 import java.io.FileInputStream ; 42 43 44 52 public class FileUtils 53 { 54 57 58 61 protected static FileUtils s_instance = new FileUtils(); 62 63 66 67 70 71 74 75 78 79 80 81 84 85 88 protected FileUtils() 89 { 90 } 92 93 94 95 96 99 public static FileUtils getInstance() 100 { 101 return s_instance; 102 } 103 104 105 106 109 110 115 public void copyFileToDirectory( File source, File dir ) 116 throws IOException 117 { 118 copyFileToFile( source, new File ( dir, source.getName() ) ); 119 } 120 121 122 126 public void copyFileToFile( File source, File target ) 127 throws IOException 128 { 129 FileInputStream fis = null; 130 FileOutputStream fos = null; 131 BufferedInputStream bis = null; 132 BufferedOutputStream bos = null; 133 134 try 135 { 136 fis = new FileInputStream ( source ); 137 fos = new FileOutputStream ( target ); 138 bis = new BufferedInputStream ( fis ); 139 bos = new BufferedOutputStream ( fos ); 140 141 byte buffer[] = new byte[ 4096 ]; 142 int size = bis.read( buffer, 0, 4096 ); 143 while (size > 0) 144 { 145 bos.write( buffer, 0, size ); 146 size = bis.read( buffer, 0, 4096 ); 147 } 148 } 149 finally 150 { 151 if (bis != null) bis.close(); 152 if (bos != null) bos.close(); 153 if (fis != null) fis.close(); 154 if (fos != null) fos.close(); 155 } 156 } 157 158 159 167 public File [] findFileRecurse( File baseDir, FilenameFilter filter ) 168 { 170 if (baseDir == null) 171 { 172 throw new IllegalArgumentException ( "no null arguments" ); 173 } 174 if (filter == null) 175 { 176 filter = new AllFilenameFilter(); 177 } 178 179 Stack dirStack = new Stack (); 180 Vector files = new Vector (); 181 182 dirStack.push( baseDir ); 183 184 while (!dirStack.empty()) 185 { 186 baseDir = (File )dirStack.pop(); 187 String [] list = baseDir.list( filter ); 188 for (int i = list.length; --i >= 0;) 189 { 190 File f = new File ( baseDir, list[i] ); 191 if (f.isDirectory()) 192 { 193 dirStack.push( f ); 194 } 195 else 196 if (f.isFile()) 197 { 198 files.addElement( f ); 199 } 200 } 201 } 202 203 File [] flist = new File [ files.size() ]; 204 files.copyInto( flist ); 205 return flist; 206 } 207 208 209 212 213 216 217 } 218 219 | Popular Tags |