private static boolean copyFile(String strFileA, String strFileB) {
// 使用try资源块 ,其中创建的流对象可以自动关闭
try (
FileInputStream inputStream = new FileInputStream(strFileA); // 输入流
FileOutputStream outputStream = new FileOutputStream(strFileB) // 输出流
) {
int sumAvailable = inputStream.available(); // 获取文件总量大小
System.out.println("该文件的数据总量为 -> " + sumAvailable); 69849662
byte[] bytes = new byte[1024 * 8]; // 定义字节数组
int len; // 定义每次获取的字节长度
while ((len = inputStream.read(bytes)) != -1) {
double available = inputStream.available(); // 剩余量
int progressPercent = (int) (100 - (((available * 100 / sumAvailable)))); // 进度
// 进度条
System.out.print("\r" + "[" + String.join("", Collections.nCopies(progressPercent / 2, "=")) + ">" +
String.join("", Collections.nCopies(50 - progressPercent / 2, " ")) + "] " +
progressPercent + "%");
// 将输入流读到的字节 写入 到输出流
outputStream.write(bytes, 0, len);
// 添加睡眠时间
try {
Thread.sleep(5);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
}
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
- 运行效果如下: