IOUtil.java 4.6 KB
Newer Older
zhangxiaoyan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
package util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Serializable;
import java.io.StringWriter;
import java.io.Writer;

/**
 * Created by sunhao on 17/5/9.
 * IO工具类
 */
public class IOUtil
{
    public static void close(OutputStream os) {
        if (os != null) {
            try {
                os.close();
            } catch (Exception ignored) {
            }
        }
    }

    public static void close(InputStream is) {
        if (is != null) {
            try {
                is.close();
            } catch (Exception ignored) {
            }
        }
    }

    public static void close(Writer os) {
        if (os != null) {
            try {
                os.close();
            } catch (Exception ignored) {
            }
        }
    }

    public static void close(Reader is) {
        if (is != null) {
            try {
                is.close();
            } catch (Exception ignored) {
            }
        }
    }

    public static void deleteAllFiles(File directory) {

        File[] files = directory.listFiles();
        for (File file : files) {
            if (!file.delete()) {
                // System.out.println("Cannot delete " + file);
            }
        }
    }

    public static String readString(InputStream input, String encoding)
            throws IOException {
        InputStreamReader ir = (encoding == null ? new InputStreamReader(input) : new InputStreamReader(input, encoding));
        StringWriter sw = new StringWriter();
        try {
            copy(ir, sw);
            return sw.toString();
        } finally {
            close(sw);
        }
    }

    public static long copy(InputStream in, OutputStream out)
            throws IOException {
        byte[] buffer = new byte[1024 * 4];
        int len;
        long count = 0;

        while ((len = in.read(buffer)) >= 0) {
            out.write(buffer, 0, len);
            count += len;
        }
        return count;
    }

    public static long copy(Reader in, Writer out)
            throws IOException {
        char[] buffer = new char[1024 * 4];
        int len;
        long count = 0;

        while ((len = in.read(buffer)) >= 0) {
            out.write(buffer, 0, len);
            count += len;
        }
        return count;
    }

    public static long copy(File src, File dest)
            throws IOException {
        FileInputStream fis = new FileInputStream(src);
        FileOutputStream fos = new FileOutputStream(dest);
        try {
            return copy(fis, fos);
        } finally {
            close(fis);
            close(fos);
        }
    }

    public static byte[] objToBytes(Serializable obj)
            throws IOException {
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        ObjectOutputStream oo = new ObjectOutputStream(bos);
        try {
            oo.writeObject(obj);
            return bos.toByteArray();
        } finally {
            close(oo);
            close(bos);
        }
    }

    public static Object objFromBytes(byte[] bytes)
            throws IOException, ClassNotFoundException {
        ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
        ObjectInputStream oi = new ObjectInputStream(bis);
        try {
            return oi.readObject();
        } finally {
            close(oi);
            close(bis);
        }
    }

    public static void objToStream(OutputStream os, Serializable obj)
            throws IOException {
        ObjectOutputStream oo = new ObjectOutputStream(os);
        try {
            oo.writeObject(obj);
        } finally {
            close(oo);
        }
    }

    public static Object objFromStream(InputStream is)
            throws IOException, ClassNotFoundException {
        ObjectInputStream oi = new ObjectInputStream(is);
        try {
            return oi.readObject();
        } finally {
            close(oi);
        }
    }

    public static void saveObject(File file, Serializable obj)
            throws IOException {
        objToStream(new BufferedOutputStream(new FileOutputStream(file)), obj);
    }

    public static Object loadObject(File file)
            throws IOException, ClassNotFoundException {
        return objFromStream(new BufferedInputStream(new FileInputStream(file)));
    }
}