1 15 16 package com.jdon.util; 17 18 import java.io.*; 19 20 21 public class FileUtil { 22 23 private static String ENCODING = "UTF-8"; 24 25 31 public static void createFile(String output, String content) throws Exception { 32 OutputStreamWriter fw = null; 33 PrintWriter out = null; 34 try { 35 if (ENCODING == null) 36 ENCODING = PropsUtil.ENCODING; 37 38 fw = new OutputStreamWriter(new FileOutputStream( 39 output), ENCODING); 40 out = new PrintWriter(fw); 41 out.print(content); 42 } catch (Exception ex) { 43 throw new Exception (ex); 44 }finally{ 45 if (out != null) 46 out.close(); 47 if (fw != null) 48 fw.close(); 49 } 50 51 } 52 53 59 public static String readFile(String input) throws Exception { 60 char[] buffer = new char[4096]; 61 int len = 0; 62 StringBuffer content = new StringBuffer (4096); 63 64 if (ENCODING == null) 65 ENCODING = PropsUtil.ENCODING; 66 InputStreamReader fr = null; 67 BufferedReader br = null; 68 try { 69 fr = new InputStreamReader(new FileInputStream(input), ENCODING); 70 br = new BufferedReader(fr); 71 while ( (len = br.read(buffer)) > -1) { 72 content.append(buffer, 0, len); 73 } 74 } catch (Exception e) { 75 throw new Exception (e); 76 }finally{ 77 if (br != null) 78 br.close(); 79 if (fr != null) 80 fr.close(); 81 } 82 return content.toString(); 83 } 84 85 92 public static void move(String input, String output) throws Exception { 93 File inputFile = new File(input); 94 File outputFile = new File(output); 95 try { 96 inputFile.renameTo(outputFile); 97 } catch (Exception ex) { 98 throw new Exception ("Can not mv" + input + " to " + output + 99 ex.getMessage()); 100 } 101 } 102 103 109 public static boolean copy(String input, String output) throws Exception { 110 int BUFSIZE = 65536; 111 FileInputStream fis = new FileInputStream(input); 112 FileOutputStream fos = new FileOutputStream(output); 113 114 try { 115 int s; 116 byte[] buf = new byte[BUFSIZE]; 117 while ( (s = fis.read(buf)) > -1) { 118 fos.write(buf, 0, s); 119 } 120 121 } catch (Exception ex) { 122 throw new Exception ("makehome" + ex.getMessage()); 123 } finally { 124 fis.close(); 125 fos.close(); 126 } 127 return true; 128 } 129 130 135 public static void makehome(String home) throws Exception { 136 File homedir = new File(home); 137 if (!homedir.exists()) { 138 try { 139 homedir.mkdirs(); 140 } catch (Exception ex) { 141 throw new Exception ("Can not mkdir :" + home + 142 " Maybe include special charactor!"); 143 } 144 } 145 } 146 147 148 154 public static void CopyDir(String sourcedir, String destdir) throws Exception { 155 File dest = new File(destdir); 156 File source = new File(sourcedir); 157 158 String [] files = source.list(); 159 try { 160 makehome(destdir); 161 } catch (Exception ex) { 162 throw new Exception ("CopyDir:" + ex.getMessage()); 163 } 164 165 for (int i = 0; i < files.length; i++) { 166 String sourcefile = source + File.separator + files[i]; 167 String destfile = dest + File.separator + files[i]; 168 File temp = new File(sourcefile); 169 if (temp.isFile()) { 170 try { 171 copy(sourcefile, destfile); 172 } catch (Exception ex) { 173 throw new Exception ("CopyDir:" + ex.getMessage()); 174 } 175 } 176 } 177 } 178 179 184 public static void recursiveRemoveDir(File directory) throws Exception { 185 if (!directory.exists()) 186 throw new IOException(directory.toString() + " do not exist!"); 187 188 String [] filelist = directory.list(); 189 File tmpFile = null; 190 for (int i = 0; i < filelist.length; i++) { 191 tmpFile = new File(directory.getAbsolutePath(), filelist[i]); 192 if (tmpFile.isDirectory()) { 193 recursiveRemoveDir(tmpFile); 194 } else if (tmpFile.isFile()) { 195 try { 196 tmpFile.delete(); 197 } catch (Exception ex) { 198 throw new Exception (tmpFile.toString() + " can not be deleted " + 199 ex.getMessage()); 200 } 201 } 202 } 203 try { 204 directory.delete(); 205 } catch (Exception ex) { 206 throw new Exception (directory.toString() + " can not be deleted " + 207 ex.getMessage()); 208 } finally { 209 filelist = null; 210 } 211 } 212 213 } 214 | Popular Tags |