IO拷贝代码

本文详细介绍了Java中四种不同的文件拷贝方式:字节流拷贝(包括图片和视频)、字符流拷贝(仅限文本文件)、缓冲字节流拷贝及缓冲字符流拷贝。通过示例代码展示了每种拷贝方式的实现,并对比了它们的效率和适用场景。重点关注了缓冲流在提高拷贝速度方面的作用。
摘要由CSDN通过智能技术生成

IO拷贝代码:

一、文件字节流拷贝方式

1.字节流拷贝图片:

	// 图片的字节流拷贝
    public static void copy() {
        FileInputStream fis = null;
        FileOutputStream fos = null;

        try {
            fis = new FileInputStream("g:/dog.jpg");
            fos = new FileOutputStream("g:/dog2.jpg");

            System.out.println("图片正在拷贝");
            int res = 0;
            while ((res = fis.read()) != -1) {
                fos.write(res);
            }
            System.out.println("图片拷贝成功");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != fos) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != fis) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

2.视频的字节流拷贝方式一(以单个字节形式进行拷贝)

	// 视频拷贝的字节流方式
    // 以单个字节的形式进行拷贝
    public static void copyVideo1() {
        System.out.println("以字节流方式进行拷贝,考完一个字节完成再拷贝另外一个,会特别慢,不建议");
        FileInputStream fis = null;
        FileOutputStream fos = null;

        try {
            fis = new FileInputStream("g:/111111.mp4");
            fos = new FileOutputStream("g:/(111111).mp4");

            System.out.println("正在以方式一拷贝");
            int res = 0;
            while ((res = fis.read()) != -1) {
                fos.write(res);
            }
            System.out.println("拷贝成功");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != fos) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != fis) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

3.视频的字节流拷贝方式二(申请视频(文件)大小的一维数组缓冲区)

	// 以一维数组作为缓冲区进行拷贝
    // 缺点:若文件过大,无法申请太大的一维数组缓冲区
    public static void copyVideo2() {
        FileInputStream fis = null;
        FileOutputStream fos = null;

        try {
            fis = new FileInputStream("g:/111111.mp4");
            fos = new FileOutputStream("g:/222222.mp4");
            System.out.println("正在玩命地拷贝...");
            int len = fis.available();
            byte[] bArr = new byte[len];

            // 不可缺少,读取文件进如字节数组
            fis.read(bArr);
            fos.write(bArr);
            System.out.println("拷贝文件成功!");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 4.关闭流对象并释放有关的资源
            if (null != fos) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != fis) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

4.视频的字节流拷贝方式三(固定缓冲区大小,分多次拷贝)

// 固定缓冲区大小,分多次拷贝
    public static void copyVideo3() {
        long l1 = System.currentTimeMillis();
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            fis = new FileInputStream("g:/111111.mp4");
            fos = new FileOutputStream("g:/333333.mp4");

            System.out.println("正在以第三种方式拷贝");
            int res = 0;
            // 正常申请1024的倍数的缓冲区大小
            byte[] bArr = new byte[1024];
            while((res=fis.read(bArr)) != -1) {
                fos.write(bArr, 0, res);
            }
            System.out.println("拷贝成功");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != fos) {
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != fis) {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
        long l2 = System.currentTimeMillis();
        System.out.println("拷贝时间为" + (l2 - l1));
    }

二、文件字符流拷贝方式

1.文件字符流拷贝文件(只能处理文本文件)

/**
 * 通过字符流实现文件拷贝
 * 字符流只能处理文本文件
 * 无法拷贝图片
 */
public class FileCharCopyTest {

    public static void copy() {
        FileReader fr = null;
        FileWriter fw = null;
        try {
            fr = new FileReader("g:/a.txt");

            fw = new FileWriter("g:/b.txt");

            System.out.println("文件正在拷贝");
            int res = 0;
            while((res = fr.read()) != -1) {
                fw.write(res);
            }
            System.out.println("文件拷贝完成");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 通常情况先创建的后关闭,后创建的先关闭
            if (null != fw) {
                try {
                    fw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != fr) {
                try {
                    fr.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }

    public static void main(String[] args) {
        FileCharCopyTest.copy();
    }
}

三、缓冲字节流拷贝方式

1.视频的字节流拷贝方式四( 缓冲字节流拷贝视频(通常使用方式)

// 在外部添加缓冲区的方式(通常使用方式)
    public static void copy1() {
        // 获取当前时间对于1970年的毫秒数
        long l1 = System.currentTimeMillis();

        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
             bis = new BufferedInputStream(new FileInputStream("g:/111111.mp4"));
             bos = new BufferedOutputStream(new FileOutputStream("g:/(111111).mp4"));

            System.out.println("正在以缓冲流的方式进行拷贝");
            byte[] bArr = new byte[1024];
            int res = 0;
            while ((res=bis.read(bArr)) != -1) {
                bos.write(bArr, 0, res);
            }
            System.out.println("拷贝完成");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != bos) {
                try {
                    bos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != bis) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        long l2 = System.currentTimeMillis();
        System.out.println("拷贝时间为" + (l2 - l1));
    }

四、缓冲字符流拷贝方式

1.缓冲字符流拷贝文件

public static void copy1() {
        BufferedReader br = null;
        BufferedWriter bw = null;

        try {
            br = new BufferedReader(new FileReader("g:/a.txt"));
            bw = new BufferedWriter(new FileWriter("g:/c.txt"));

            System.out.println("正在拷贝");
            String str = null;
            while ((str=br.readLine()) != null) {
                bw.write(str);
                // 用于写于行分隔符到输出流中
                bw.newLine();
            }
            System.out.println("拷贝成功");

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != bw) {
                try {
                    bw.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (null != br) {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

        }
    }
Java中实现深拷贝的方法有很多种,这里介绍其中两种较常用的方式: 1. 通过实现Serializable接口实现对象的深拷贝 参考代码如下: ``` import java.io.*; public class DeepCopy implements Serializable { private static final long serialVersionUID = 1L; private String name; private Age age; public DeepCopy(String name, int year, int value) { this.name = name; this.age = new Age(year, value); } public DeepCopy deepCopy() throws IOException, ClassNotFoundException, OptionalDataException { // 将对象写入流中 ByteArrayOutputStream bos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(bos); oos.writeObject(this); // 从流中读出对象 ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ObjectInputStream ois = new ObjectInputStream(bis); return (DeepCopy) ois.readObject(); } @Override public String toString() { return "DeepCopy{" + "name='" + name + '\'' + ", age=" + age + '}'; } public static void main(String[] args) throws ClassNotFoundException, IOException { DeepCopy deepCopy = new DeepCopy("Tom", 1990, 30); DeepCopy deepCopy1 = deepCopy.deepCopy(); System.out.println(deepCopy); System.out.println(deepCopy1); System.out.println(deepCopy.age == deepCopy1.age); // false,说明深拷贝成功 } private static class Age implements Serializable { private static final long serialVersionUID = 2L; private int year; private int value; public Age(int year, int value) { this.year = year; this.value = value; } @Override public String toString() { return "Age{" + "year=" + year + ", value=" + value + '}'; } } } ``` 2. 通过clone()方法实现对象的深拷贝 参考代码如下: ``` public class DeepCopy implements Cloneable { private String name; private Age age; public DeepCopy(String name, int year, int value) { this.name = name; this.age = new Age(year, value); } @Override public DeepCopy clone() throws CloneNotSupportedException { DeepCopy deepCopy = (DeepCopy) super.clone(); deepCopy.age = age.clone(); return deepCopy; } @Override public String toString() { return "DeepCopy{" + "name='" + name + '\'' + ", age=" + age + '}'; } public static void main(String[] args) throws CloneNotSupportedException { DeepCopy deepCopy = new DeepCopy("Tom", 1990, 30); DeepCopy deepCopy1 = deepCopy.clone(); System.out.println(deepCopy); System.out.println(deepCopy1); System.out.println(deepCopy.age == deepCopy1.age); // false,说明深拷贝成功 } private static class Age implements Cloneable { private int year; private int value; public Age(int year, int value) { this.year = year; this.value = value; } @Override public Age clone() throws CloneNotSupportedException { return (Age) super.clone(); } @Override public String toString() { return "Age{" + "year=" + year + ", value=" + value + '}'; } } } ``` --相关问题--:
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

技术很low的瓜贼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值