1. package com.lms.common.util;
    2. import java.io.*;
    3. import java.nio.channels.FileChannel;
    4. /**
    5. * @Author: 李孟帅
    6. * @Date: 2021-12-02 20:02
    7. * @Description:
    8. */
    9. public class IoUtil {
    10. public static void copy(InputStream in, OutputStream out) throws IOException {
    11. BufferedOutputStream bos = new BufferedOutputStream(out);
    12. BufferedInputStream bis = new BufferedInputStream(in);
    13. byte[] buf = new byte[1024];
    14. int len;
    15. while ((len = bis.read(buf)) != -1) {
    16. bos.write(buf, 0, len);
    17. }
    18. }
    19. public static void copy(FileInputStream in, FileOutputStream out) throws IOException {
    20. FileChannel from = in.getChannel();
    21. FileChannel to = out.getChannel();
    22. long size = from.size();
    23. for (long left = size; left > 0; ) {
    24. left -= from.transferTo(size - left, left, to);
    25. }
    26. }
    27. public static void copy(File src, File dest) throws IOException {
    28. FileInputStream in = new FileInputStream(src);
    29. FileOutputStream out = new FileOutputStream(dest);
    30. copy(in, out);
    31. close(in);
    32. close(out);
    33. }
    34. public static void close(Closeable closeable) {
    35. if (null != closeable) {
    36. try {
    37. closeable.close();
    38. } catch (Exception ignored) {
    39. }
    40. }
    41. }
    42. }