1 19 20 package com.maverick.util; 21 22 import java.io.File ; 24 import java.io.FileInputStream ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.OutputStream ; 29 30 35 public class IOUtil { 36 37 40 public static int BUFFER_SIZE = 8192; 41 42 50 public static void copy(InputStream in, OutputStream out) throws IOException { 51 copy(in, out, -1); 52 } 53 54 55 64 public static void copy(InputStream in, OutputStream out, long count) throws IOException { 65 copy(in, out, count, BUFFER_SIZE); 66 } 67 68 78 public static void copy(InputStream in, OutputStream out, long count, int bufferSize) throws IOException { 79 byte buffer[] = new byte[bufferSize]; 80 int i = bufferSize; 81 if (count >= 0) { 82 while (count > 0) { 83 if (count < bufferSize) 84 i = in.read(buffer, 0, (int) count); 85 else 86 i = in.read(buffer, 0, bufferSize); 87 88 if (i == -1) 89 break; 90 91 count -= i; 92 out.write(buffer, 0, i); 93 } 94 } else { 95 while (true) { 96 i = in.read(buffer, 0, bufferSize); 97 if (i < 0) 98 break; 99 out.write(buffer, 0, i); 100 } 101 } 102 } 103 110 public static boolean closeStream(InputStream in) { 111 try { 112 if (in != null) { 113 in.close(); 114 } 115 116 return true; 117 } 118 catch (IOException ioe) { 119 return false; 120 } 121 } 122 123 130 public static boolean closeStream(OutputStream out) { 131 try { 132 if (out != null) { 133 out.close(); 134 } 135 136 return true; 137 } 138 catch (IOException ioe) { 139 return false; 140 } 141 } 142 143 public static boolean delTree(File file) { 144 if (file.isFile()) { 145 return file.delete(); 146 } 147 else { 148 String [] list = file.list(); 149 for (int i = 0; i < list.length; i++) { 150 if (!delTree(new File (file, list[i]))) { 151 return false; 152 } 153 } 154 } 155 return true; 156 } 157 158 public static void recurseDeleteDirectory(File dir) { 159 160 String [] files = dir.list(); 161 162 if (files == null) { 163 return; } 165 166 for (int i = 0; i < files.length; i++) { 167 File f = new File (dir, files[i]); 168 169 if (f.isDirectory()) { 170 recurseDeleteDirectory(f); 171 172 } 173 f.delete(); 174 } 175 176 dir.delete(); 177 178 } 179 180 public static void copyFile(File from, File to) throws IOException { 181 182 if (from.isDirectory()) { 183 if (!to.exists()) { 184 to.mkdir(); 185 } 186 String [] children = from.list(); 187 for (int i = 0; i < children.length; i++) { 188 File f = new File (from, children[i]); 189 if (f.getName().equals(".") 190 || f.getName().equals("..")) { 191 continue; 192 } 193 if (f.isDirectory()) { 194 File f2 = new File (to, f.getName()); 195 copyFile(f, f2); 196 } 197 else { 198 copyFile(f, to); 199 } 200 } 201 } 202 else if (from.isFile() && (to.isDirectory() || to.isFile())) { 203 if (to.isDirectory()) { 204 to = new File (to, from.getName()); 205 } 206 FileInputStream in = new FileInputStream (from); 207 FileOutputStream out = new FileOutputStream (to); 208 byte[] buf = new byte[32678]; 209 int read; 210 while ( (read = in.read(buf)) > -1) { 211 out.write(buf, 0, read); 212 } 213 closeStream(in); 214 closeStream(out); 215 216 } 217 } 218 219 } 220 | Popular Tags |