1 package com.hxl;
2
3 import java.io.BufferedInputStream;
4 import java.io.BufferedOutputStream;
5 import java.io.FileInputStream;
6 import java.io.FileOutputStream;
7 import java.io.IOException;
8
9 public class Test {
10 public static void main(String[] args) throws IOException {
11 long start = System.currentTimeMillis();
12 // 测试文件大小为3.12MB
13 // 运行耗时:21707毫秒
14 method1("E:\\EditPlus4.3安装程序及说明.zip", "C:\\Users\\Administrator\\Desktop\\EditPlus4.3安装程序及说明.zip");
15 // 运行耗时:76毫秒
16 // method2("E:\\EditPlus4.3安装程序及说明.zip", "C:\\Users\\Administrator\\Desktop\\EditPlus4.3安装程序及说明.zip");
17 // 运行耗时:294毫秒
18 // method3("E:\\EditPlus4.3安装程序及说明.zip", "C:\\Users\\Administrator\\Desktop\\EditPlus4.3安装程序及说明.zip");
19 // 运行耗时:54毫秒
20 // method4("E:\\EditPlus4.3安装程序及说明.zip", "C:\\Users\\Administrator\\Desktop\\EditPlus4.3安装程序及说明.zip");
21 long end = System.currentTimeMillis();
22 System.out.println("运行耗时:" + (end - start) + "毫秒");
23 }
24
25 // 基本字节流一次读取一个字节复制文件
26 public static void method1(String src, String dest) throws IOException {
27 // 数据源
28 FileInputStream fis = new FileInputStream(src);
29 // 目的位置
30 FileOutputStream fos = new FileOutputStream(dest, true);
31 // read()方法返回值为获取到的那个字符的ASCII码值,若没有获取到,则返回-1
32 int byt = 0;
33 while ((byt = fis.read()) != -1) {
34 // write(int
35 // b)方法指的是将字节数据(主要因为read的返回值范围是0~255和-1,而write此时传参若为byte显然不合适了)写入(复制到)文件
36 // 经测试知,传-1显示了一段空格(不是Tab建),可以传0~255和-1以外的int值,但用记事本打开是乱码(以'?'表示)
37 fos.write(byt);
38 }
39 // 关闭输出流
40 fos.close();
41 // 关闭输入流
42 fis.close();
43 }
44
45 // 基本字节流一次读取一个字节数组复制文件
46 public static void method2(String src, String dest) throws IOException {
47 // 数据源
48 FileInputStream fis = new FileInputStream(src);
49 // 目的位置
50 FileOutputStream fos = new FileOutputStream(dest);
51 // 定义单次读取字节数组的大小,一般就写1024
52 byte[] bys = new byte[1024];
53 // read(byte[] bys)方法返回值为获取到的字节个数,若没有获取到,则返回-1
54 int length = 0;
55 while ((length = fis.read(bys)) != -1) {
56 // write(byte[] bys,int off,int length)方法指的是从指定字节数组的指定位置开始写入(复制到)文件
57 fos.write(bys, 0, length);
58 }
59 // 关闭输出流
60 fos.close();
61 // 关闭输入流
62 fis.close();
63 }
64
65 // 高效字节流一次读取一个字节复制文件
66 public static void method3(String src, String dest) throws IOException {
67 // 数据源
68 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
69 // 目的位置
70 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest));
71 // read()方法返回值为获取到的那个字符的ASCII码值,若没有获取到,则返回-1
72 int byt = 0;
73 while ((byt = bis.read()) != -1) {
74 // write(int b)方法指的是将字节数据(主要因为read的返回值范围是0~255和-1,而write此时传参若为byte显然不合适了)写入(复制到)文件
75 // 经测试知,传-1显示了一段空格(不是Tab建),可以传0~255和-1以外的int值,但用记事本打开是乱码(以'?'表示)
76 bos.write(byt);
77 }
78 // 关闭输出流
79 bos.close();
80 // 关闭输入流
81 bis.close();
82 }
83
84 // 高效字节流一次读取一个字节数组复制文件
85 public static void method4(String src, String dest) throws IOException {
86 // 数据源
87 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(src));
88 // 目的位置
89 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(dest));
90 // 定义单次读取字节数组的大小,一般就写1024
91 byte[] bys = new byte[1024];
92 // read(byte[] bys)方法返回值为获取到的字节个数,若没有获取到,则返回-1
93 int length = 0;
94 while ((length = bis.read(bys)) != -1) {
95 // write(byte[] bys,int off,int length)方法指的是从指定字节数组的指定位置开始写入(复制到)文件
96 bos.write(bys, 0, length);
97 }
98 // 关闭输出流
99 bos.close();
100 // 关闭输入流
101 bis.close();
102 }
103 }