Java日志第44天 2020.8.18

综合实例

本案例主要写一个工具类,该工具类主要实现读写文件和复制文件功能。

复制代码
import com.sun.xml.internal.bind.v2.model.runtime.RuntimeReferencePropertyInfo;

import java.io.*;

public class IOUtils {
    //IO的工具类
    public static void print(byte[] ary) {
        for (int b : ary) {
            b &= 0xff;
            if(b <= 0xf){
                System.out.print("0");
            }
            System.out.print(Integer.toHexString(b)+" ");
        }
        System.out.println();
    }

    public static byte[] read(String file){
        try{
            InputStream in = new FileInputStream(file);
            byte[] buf = new byte[in.available()];
            in.read(buf);
            in.close();
            return buf;
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public static Object deepCopy(Object obj) {
        try {
            ByteArrayOutputStream buf = new ByteArrayOutputStream();
            ObjectOutputStream oos = new ObjectOutputStream(buf);
            oos.writeObject(obj);
            oos.close();
            byte[] ary = buf.toByteArray();
            ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(ary));
            Object o = ois.readObject();
            ois.close();
            return o;
        } catch (Exception e){
            throw new RuntimeException(e);
        }
    }

    public static void cp(File from, File to){
        try {
            InputStream in = new FileInputStream(from);
            OutputStream out = new FileOutputStream(to);
            byte[] buf = new byte[1024];
            int len;
            while((len = in.read(buf)) != -1) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
        } catch (IOException e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public static void cp1(File from, File to) {
        try {
            InputStream in = new FileInputStream(from);
            OutputStream out = new FileOutputStream(to);
            int len;
            while((len = in.read()) != -1) {
                out.write(len);
            }
            in.close();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public static void cp(String from, String to) {
        cp(new File(from), new File(to));
    }

    //将文件按照16进制形式打印到控制台,每16个byte为一行
    public static void print(File file) {
        try {
            InputStream in = new FileInputStream(file);
            int len;
            int i = 1;
            while((len = in.read()) != -1) {
                if(len <= 0xf)
                    System.out.print("0");
                System.out.print(Integer.toHexString(len)+" ");
                if (i++ % 16 ==0){
                    System.out.println();
                }
            }
            System.out.println();
            in.close();
        } catch (IOException e){
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    public static void print(String file) {
        print(new File(file));
    }

    /*
        将文件切分为指定大小的系列文件
        文件大小以k为单位
     */
    public static void split(String file, int size) {
        try {
            if(size <= 0) {
                throw new IllegalArgumentException("搞啥呀!");
            }
            int idx = 0;//文件的序号
            InputStream in = new BufferedInputStream(new FileInputStream(file));
            OutputStream out = new BufferedOutputStream(new FileOutputStream(file + "." + idx++));
            int b;
            int count = 0;
            while((b = in.read()) != -1){
                out.write(b);
                count++;
            }
            if(count % (size * 1024) == 0) {
                out.close();
                out = new BufferedOutputStream(new FileOutputStream(file + "." + idx++));
            }
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }
    
    public static void join(String file) {
        try {
            String fileName = file.substring(0,file.lastIndexOf("."));
            String num = file.substring(file.lastIndexOf(".")+1);
            int idx = Integer.parseInt(num);
            OutputStream out = new FileOutputStream(fileName);
            File f = new File(fileName + "." + idx++);
            while(f.exists()) {
                InputStream in = new FileInputStream(f);
                cp(in, out);
                in.close();
                f= new File(fileName + "." + idx++);
            }
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }
    
    //复制文件,复制in到out,不关闭流
    public static void cp(InputStream in,OutputStream out) throws IOException {
        byte[] buf = new byte[1024*512];//512K缓冲
        int count;
        while((count = in.read(buf)) != -1) {
            out.write(buf,0,count);
        }
        out.flush();
    }
}
复制代码

 

posted @   Gazikel  阅读(116)  评论(0编辑  收藏  举报
编辑推荐:
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
点击右上角即可分享
微信分享提示