一、前言
关于dyuproject的dyuproject-util工具包com.dyuproject.util.ResourceUtil资源工具类,对文件读取二机制数组处理readBytes、通过输入输出文件流Stream、文件File、URL链接资源复制迁移copy、文件目录复制迁移copyFileToDir等。
二、源码说明
package com.dyuproject.util;@b@@b@import java.io.ByteArrayOutputStream;@b@import java.io.File;@b@import java.io.FileInputStream;@b@import java.io.FileOutputStream;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.OutputStream;@b@import java.net.URI;@b@import java.net.URL;@b@import java.net.URLConnection;@b@import java.util.ArrayList;@b@import java.util.Collections;@b@import java.util.List;@b@@b@public final class ResourceUtil@b@{@b@ public static int __copyBufferSize = 4096;@b@@b@ public static void setCopyBufferSize(int size)@b@ {@b@ __copyBufferSize = size;@b@ }@b@@b@ public static int getCopyBufferSize()@b@ {@b@ return __copyBufferSize;@b@ }@b@@b@ public static byte[] readBytes(File file) throws IOException@b@ {@b@ return readBytes(file.toURI());@b@ }@b@@b@ public static byte[] readBytes(String file) throws IOException@b@ {@b@ return readBytes(URI.create(file));@b@ }@b@@b@ public static byte[] readBytes(URI file) throws IOException@b@ {@b@ return readBytes(file.toURL());@b@ }@b@@b@ public static byte[] readBytes(URL file) throws IOException@b@ {@b@ return readBytes(file.openStream());@b@ }@b@@b@ public static byte[] readBytes(InputStream in) throws IOException@b@ {@b@ return getByteArrayOutputStream(in).toByteArray();@b@ }@b@@b@ public static ByteArrayOutputStream getByteArrayOutputStream(InputStream in) throws IOException@b@ {@b@ ByteArrayOutputStream out = new ByteArrayOutputStream();@b@ byte[] buffer = new byte[__copyBufferSize];@b@ int length = 0;@b@ while ((length = in.read(buffer, 0, __copyBufferSize)) != -1)@b@ out.write(buffer, 0, length); try {@b@ in.close(); } catch (Exception e) { }@b@ return out;@b@ }@b@@b@ public static void copy(URL in, URL out) throws IOException@b@ {@b@ copy(in.openStream(), out.openConnection().getOutputStream());@b@ }@b@@b@ public static void copy(File in, File out) throws IOException@b@ {@b@ copy(new FileInputStream(in), new FileOutputStream(out));@b@ }@b@@b@ public static void copy(URL in, File out) throws IOException@b@ {@b@ copy(in.openStream(), new FileOutputStream(out));@b@ }@b@@b@ public static void copy(InputStream in, OutputStream out) throws IOException@b@ {@b@ byte[] buffer = new byte[__copyBufferSize];@b@ int length = 0;@b@ while ((length = in.read(buffer, 0, __copyBufferSize)) != -1)@b@ out.write(buffer, 0, length); try {@b@ out.close(); } catch (Exception e) {@b@ }@b@ }@b@@b@ public static void copy(InputStream in, OutputStream out, int bufferSize) throws IOException {@b@ byte[] buffer = new byte[bufferSize];@b@ int length = 0;@b@ while ((length = in.read(buffer, 0, bufferSize)) != -1)@b@ out.write(buffer, 0, length); try {@b@ out.close(); } catch (Exception e) {@b@ }@b@ }@b@@b@ static void copyDir(File dirFrom, File dirTo) throws IOException {@b@ File[] files = dirFrom.listFiles();@b@ if (!(dirTo.exists()))@b@ dirTo.mkdirs();@b@ for (int i = 0; i < files.length; ++i)@b@ {@b@ File f = files[i];@b@ if (f.isDirectory())@b@ copyDir(f, new File(dirTo, f.getName()));@b@ else@b@ copy(f, new File(dirTo, f.getName()));@b@ }@b@ }@b@@b@ public static void copyFileToDir(File file, File dirTo) throws IOException@b@ {@b@ if (file.exists())@b@ {@b@ if (file.isDirectory()) {@b@ copyDir(file, dirTo);@b@ }@b@ else {@b@ if (!(dirTo.exists()))@b@ dirTo.mkdirs();@b@ copy(file, new File(dirTo, file.getName()));@b@ }@b@ }@b@ }@b@@b@ public static List<File> getFilesByExtension(File dir, String[] extensions)@b@ {@b@ if ((!(dir.isDirectory())) || (extensions == null))@b@ return Collections.emptyList();@b@ List files = new ArrayList();@b@ addFilesByExtension(files, dir, extensions);@b@ return files;@b@ }@b@@b@ static void addFilesByExtension(List<File> list, File dir, String[] extensions)@b@ {@b@ File[] files = dir.listFiles();@b@ for (int i = 0; i < files.length; ++i)@b@ {@b@ String[] arr$;@b@ int i$;@b@ File f = files[i];@b@ if (f.isDirectory()) {@b@ addFilesByExtension(list, f, extensions);@b@ }@b@ else {@b@ arr$ = extensions; int len$ = arr$.length; for (i$ = 0; i$ < len$; ++i$) { String s = arr$[i$];@b@@b@ if (f.getName().endsWith(s))@b@ {@b@ list.add(f);@b@ break;@b@ }@b@ }@b@ }@b@ }@b@ }@b@}