Java中使用字节流可以拷贝任何格式的文件,在这里使用四种方法测试拷贝文件耗时。
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
/**************************************************************************************************
* @copyright 2023-2022
* @package
* @file TestCopyFileByBytes.java
* @date 2023-04-26 14:04
* @author qiao wei
* @version 1.0
* @brief 四种拷贝文件方式,测试耗时
* @history
*************************************************************************************************/
public class TestCopyFileByBytes {
public static void main(String[] args) {
long startTime = System.currentTimeMillis();
// copyMethod01();
// copyMethod02();
copyMethod04();
long endTime = System.currentTimeMillis();
System.out.println((endTime - startTime) / 1000.0 + "秒");
}
/**********************************************************************************************
* @class IOTest02
* @date 2023-04-22 10:47
* @author qiao wei
* @version 1.0
* @brief 字节流的基本流方法,1次读取1个字节进行文件拷贝。测试文件为ubuntu-22.04.2-desktop-amd64.iso,文
* 件大小为4,927,586,304个字节。每次读取1个字节,最耗时
* @param
* @return
* @throws
*********************************************************************************************/
private static void copyMethod01() {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
// 创建输入、输出字节流实例
fis = new FileInputStream(
new File("D:\\download\\ubuntu-22.04.2-desktop-amd64.iso")
);
fos = new FileOutputStream(
new File("D:\\download\\ubuntu.iso")
);
int b = 0;
while (-1 != (b = fis.read())) {
fos.write(b);
}
} catch (FileNotFoundException exception) {
exception.printStackTrace();
} catch (IOException exception) {
exception.printStackTrace();
} finally {
if (null != fos) {
try {
fos.close();
} catch (IOException exception) {
exception.printStackTrace();
}
}
if (null != fis) {
try {
fis.close();
} catch (IOException exception) {
exception.printStackTrace();
}
}
}
}
/**********************************************************************************************
* @class IOTest02
* @date 2023-04-22 11:07
* @author qiao wei
* @version 1.0
* @brief 字节流的基本流方法,1次读取1个字节数组进行文件拷贝。测试文件为ubuntu-22.04.2-desktop-amd64.iso,
* 文件大小为4,927,586,304个字节。设置5M大小的字节数组最为缓存区,测试拷贝耗时最少5秒
* @param
* @return
* @throws
*********************************************************************************************/
private static void copyMethod02() {
FileInputStream fis = null;
FileOutputStream fos = null;
try {
// 创建输入、输出字节流实例
fis = new FileInputStream(
new File("D:\\download\\ubuntu-22.04.2-desktop-amd64.iso")
);
fos = new FileOutputStream(
new File("D:\\download\\ubuntu_copyMethod02.iso")
);
// 5M数组
byte[] bytes = new byte[1024 * 1024 * 5];
int byteLength = 0;
while (-1 != (byteLength = fis.read(bytes))) {
fos.write(bytes, 0, byteLength);
}
} catch (FileNotFoundException exception) {
exception.printStackTrace();
} catch (IOException exception) {
exception.printStackTrace();
} finally {
if (null != fos) {
try {
fos.close();
} catch (IOException exception) {
exception.printStackTrace();
}
}
if (null != fis) {
try {
fis.close();
} catch (IOException exception) {
exception.printStackTrace();
}
}
}
}
/**********************************************************************************************
* @class IOTest02
* @date 2023-04-22 15:13
* @author qiao wei
* @version 1.0
* @brief 字节流的基本流方法。使用输入、输出缓冲流对输入、输出字节流进行包装,同时设置字节数组,1次读取5M个字节
* 进行文件拷贝。测试文件为ubuntu-22.04.2-desktop-amd64.iso,文件大小为4,927,586,304个字节。
* 效率与copyMethod04()相同,拷贝耗时最少5秒
* @param
* @return
* @throws
*********************************************************************************************/
private static void copyMethod04() {
// 基本字节流
FileInputStream fis = null;
FileOutputStream fos = null;
// 缓冲字节流
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
// 将字节流包装
fis = new FileInputStream(
new File("D:\\download\\ubuntu-22.04.2-desktop-amd64.iso")
);
bis = new BufferedInputStream(fis);
fos = new FileOutputStream(
new File("D:\\download\\ubuntu_copyMethod04.iso")
);
bos = new BufferedOutputStream(fos);
// 5M数组
byte[] bytes = new byte[1024 * 1024 * 5];
int byteLength = 0;
while (-1 != (byteLength = bis.read(bytes))) {
bos.write(bytes, 0, byteLength);
}
} catch (FileNotFoundException exception) {
exception.printStackTrace();
} catch (IOException exception) {
exception.printStackTrace();
} finally {
if (null != bos) {
try {
bos.close();
} catch (IOException exception) {
exception.printStackTrace();
}
}
if (null != bis) {
try {
bis.close();
} catch (IOException exception) {
exception.printStackTrace();
}
}
}
}
}