github.com/whatap/golib@v0.0.22/util/fileutil/FileUtil.go (about) 1 package fileutil 2 3 import ( 4 "bytes" 5 "fmt" 6 "net" 7 "os" 8 ) 9 10 func CloseOfDatagramSocket(udp *net.UDPConn) error { 11 if udp != nil { 12 return udp.Close() 13 } 14 return fmt.Errorf("%s", "UDPConn is nil") 15 } 16 17 func ExistsFile(filename string) (os.FileInfo, bool) { 18 info, err := os.Stat(filename) 19 if os.IsNotExist(err) { 20 return nil, false 21 } 22 if err != nil { 23 return nil, false 24 } 25 return info, !info.IsDir() 26 } 27 28 func ExistsDir(filename string) (os.FileInfo, bool) { 29 info, err := os.Stat(filename) 30 if os.IsNotExist(err) { 31 return nil, false 32 } 33 if err != nil { 34 return nil, false 35 } 36 return info, info.IsDir() 37 } 38 func Info(filename string) (os.FileInfo, error) { 39 info, err := os.Stat(filename) 40 if os.IsNotExist(err) { 41 return nil, err 42 } 43 return info, nil 44 } 45 46 //ReadFile get file content 47 func ReadFile(filepath string, maxlength int64) ([]byte, int64, error) { 48 f, e := os.Open(filepath) 49 if e != nil { 50 return nil, 0, e 51 } 52 defer f.Close() 53 var output bytes.Buffer 54 buf := make([]byte, 4096) 55 nbyteuntilnow := int64(0) 56 for nbyteleft := maxlength; nbyteleft > 0; { 57 nbytethistime, e := f.Read(buf) 58 if nbytethistime == 0 || e != nil { 59 break 60 } 61 nbyteleft -= int64(nbytethistime) 62 nbyteuntilnow += int64(nbytethistime) 63 output.Write(buf[:nbytethistime]) 64 } 65 66 if nbyteuntilnow > 0 { 67 return output.Bytes(), nbyteuntilnow, nil 68 } 69 70 return nil, 0, e 71 } 72 73 // 74 // public static InputStream close(InputStream in) { 75 // try { 76 // if (in != null) { 77 // in.close(); 78 // } 79 // } catch (Throwable e) { 80 // } 81 // return null; 82 // } 83 // 84 // public static OutputStream close(OutputStream out) { 85 // try { 86 // if (out != null) { 87 // out.close(); 88 // } 89 // } catch (Throwable e) { 90 // } 91 // return null; 92 // } 93 // 94 // public static Reader close(Reader in) { 95 // try { 96 // if (in != null) { 97 // in.close(); 98 // } 99 // } catch (Throwable e) { 100 // } 101 // return null; 102 // } 103 // 104 // public static Writer close(Writer out) { 105 // try { 106 // if (out != null) { 107 // out.close(); 108 // } 109 // } catch (Throwable e) { 110 // } 111 // return null; 112 // } 113 // 114 // public static byte[] readAll(InputStream fin) throws IOException { 115 // ByteArrayOutputStream out = new ByteArrayOutputStream(); 116 // byte[] buff = new byte[4096]; 117 // int n = fin.read(buff); 118 // while (n >= 0) { 119 // out.write(buff, 0, n); 120 // n = fin.read(buff); 121 // } 122 // return out.toByteArray(); 123 // } 124 // 125 // public static IClose close(IClose object) { 126 // try { 127 // if (object != null) { 128 // object.close(); 129 // } 130 // } catch (Throwable e) { 131 // e.printStackTrace(); 132 // } 133 // return null; 134 // } 135 // 136 // public static RandomAccessFile close(RandomAccessFile raf) { 137 // try { 138 // if (raf != null) { 139 // raf.close(); 140 // } 141 // } catch (Throwable e) { 142 // } 143 // return null; 144 // } 145 // 146 // public static Socket close(Socket socket) { 147 // try { 148 // if (socket != null) { 149 // socket.close(); 150 // } 151 // } catch (Throwable e) { 152 // } 153 // return null; 154 // } 155 // 156 // public static ServerSocket close(ServerSocket socket) { 157 // try { 158 // if (socket != null) { 159 // socket.close(); 160 // } 161 // } catch (Throwable e) { 162 // } 163 // return null; 164 // } 165 // 166 // public static void save(String file, byte[] b) { 167 // save(new File(file), b); 168 // } 169 // 170 // public static void save(File file, byte[] byteArray) { 171 // FileOutputStream out = null; 172 // try { 173 // out = new FileOutputStream(file); 174 // out.write(byteArray); 175 // } catch (Exception e) { 176 // } 177 // close(out); 178 // } 179 // 180 // public static byte[] readAll(File file) { 181 // FileInputStream in = null; 182 // try { 183 // in = new FileInputStream(file); 184 // return readAll(in); 185 // } catch (Exception e) { 186 // } finally { 187 // close(in); 188 // } 189 // return null; 190 // } 191 // 192 // public static void copy(File src, File dest) { 193 // try { 194 // copy(src, dest, true); 195 // } catch (Exception e) { 196 // } 197 // } 198 // 199 // public static boolean copy(File src, File dest, boolean overwrite) throws IOException { 200 // if (!src.isFile() || !src.exists()) 201 // return false; 202 // if (dest.exists()) { 203 // if (dest.isDirectory()) // Directory이면 src파일명을 사용한다. 204 // dest = new File(dest, src.getName()); 205 // else if (dest.isFile()) { 206 // if (!overwrite) 207 // throw new IOException(dest.getAbsolutePath() + "' already exists!"); 208 // } else 209 // throw new IOException("Invalid file '" + dest.getAbsolutePath() + "'"); 210 // } 211 // File destDir = dest.getParentFile(); 212 // if (!destDir.exists()) 213 // if (!destDir.mkdirs()) 214 // throw new IOException("Failed to create " + destDir.getAbsolutePath()); 215 // long fileSize = src.length(); 216 // if (fileSize > 20 * 1024 * 1024) { 217 // FileInputStream in = null; 218 // FileOutputStream out = null; 219 // try { 220 // in = new FileInputStream(src); 221 // out = new FileOutputStream(dest); 222 // int done = 0; 223 // int buffLen = 32768; 224 // byte buf[] = new byte[buffLen]; 225 // while ((done = in.read(buf, 0, buffLen)) >= 0) { 226 // if (done == 0) 227 // Thread.yield(); 228 // else 229 // out.write(buf, 0, done); 230 // } 231 // } finally { 232 // close(in); 233 // close(out); 234 // } 235 // } else { 236 // FileInputStream in = null; 237 // FileOutputStream out = null; 238 // FileChannel fin = null; 239 // FileChannel fout = null; 240 // try { 241 // in = new FileInputStream(src); 242 // out = new FileOutputStream(dest); 243 // fin = in.getChannel(); 244 // fout = out.getChannel(); 245 // long position = 0; 246 // long done = 0; 247 // long count = Math.min(65536, fileSize); 248 // do { 249 // done = fin.transferTo(position, count, fout); 250 // position += done; 251 // fileSize -= done; 252 // } while (fileSize > 0); 253 // } finally { 254 // close(fin); 255 // close(fout); 256 // close(in); 257 // close(out); 258 // } 259 // } 260 // return true; 261 // } 262 // 263 // public static FileChannel close(FileChannel fc) { 264 // if (fc != null) { 265 // try { 266 // fc.close(); 267 // } catch (IOException e) { 268 // } 269 // } 270 // return null; 271 // } 272 // 273 // // public static void chmod777(File file) { 274 // // try { 275 // // file.setReadable(true, false); 276 // // file.setWritable(true, false); 277 // // file.setExecutable(true, false); 278 // // } catch (Throwable th) {} 279 // // } 280 // public static void close(DataInputX in) { 281 // try { 282 // if (in != null) 283 // in.close(); 284 // } catch (Exception e) { 285 // } 286 // } 287 // 288 // public static void close(DataOutputX out) { 289 // try { 290 // if (out != null) 291 // out.close(); 292 // } catch (Exception e) { 293 // } 294 // } 295 // 296 // public static String load(File file, String enc) { 297 // if (file == null || file.canRead() == false) 298 // return null; 299 // BufferedInputStream in = null; 300 // try { 301 // in = new BufferedInputStream(new FileInputStream(file)); 302 // return new String(readAll(in), enc); 303 // } catch (IOException e) { 304 // e.printStackTrace(); 305 // } finally { 306 // close(in); 307 // } 308 // return null; 309 // } 310 // 311 // public static void append(String file, String line) { 312 // PrintWriter out = null; 313 // try { 314 // out = new PrintWriter(new FileWriter(file, true)); 315 // out.println(line); 316 // } catch (Exception e) { 317 // } 318 // close(out); 319 // } 320 // 321 // public static boolean mkdirs(String path) { 322 // File f = new File(path); 323 // if (f.exists() == false) 324 // return f.mkdirs(); 325 // else 326 // return true; 327 // } 328 // 329 // public static Properties readProperties(File f) { 330 // BufferedInputStream reader = null; 331 // Properties p = new Properties(); 332 // try { 333 // reader = new BufferedInputStream(new FileInputStream(f)); 334 // p.load(reader); 335 // } catch (Exception e) { 336 // } finally { 337 // close(reader); 338 // } 339 // return p; 340 // } 341 // 342 // public static void writeProperties(File f, Properties p) { 343 // PrintWriter pw = null; 344 // try { 345 // pw = new PrintWriter(f); 346 // p.list(pw); 347 // } catch (Exception e) { 348 // } finally { 349 // close(pw); 350 // } 351 // } 352 // 353 // public static void close(Connection conn) { 354 // try { 355 // if (conn != null) { 356 // conn.close(); 357 // } 358 // } catch (Throwable e) { 359 // e.printStackTrace(); 360 // } 361 // } 362 // 363 // public static void close(Statement stmt) { 364 // try { 365 // if (stmt != null) { 366 // stmt.close(); 367 // } 368 // } catch (Throwable e) { 369 // e.printStackTrace(); 370 // } 371 // } 372 // 373 // public static void close(ResultSet rs) { 374 // try { 375 // if (rs != null) { 376 // rs.close(); 377 // } 378 // } catch (Throwable e) { 379 // e.printStackTrace(); 380 // } 381 // } 382 // 383 // public static void close(JarFile jarfile) { 384 // try { 385 // if (jarfile != null) { 386 // jarfile.close(); 387 // } 388 // } catch (Throwable e) { 389 // } 390 // } 391 // 392 // public static void delete(String home, String prefix, String postfix) { 393 // File[] files = new File(home).listFiles(); 394 // for (int i = 0; i < files.length; i++) { 395 // if (files[i].isFile()) { 396 // String nm = files[i].getName(); 397 // if (nm.startsWith(prefix) && nm.endsWith(postfix)) { 398 // files[i].delete(); 399 // } 400 // } 401 // } 402 // } 403 // 404 // public static void createNew(File file, long size) { 405 // BufferedOutputStream w = null; 406 // try { 407 // w = new BufferedOutputStream(new FileOutputStream(file)); 408 // for (int i = 0; i < size; i++) { 409 // w.write(0); 410 // } 411 // } catch (Exception e) { 412 // 413 // } finally { 414 // close(w); 415 // } 416 // } 417 // 418 // public static int getCrc(File f) { 419 // BufferedInputStream in = null; 420 // try { 421 // in = new BufferedInputStream(new FileInputStream(f)); 422 // return HashUtil.hash(in, (int) f.length()); 423 // } catch (Exception e) { 424 // } finally { 425 // close(in); 426 // } 427 // return 0; 428 // }