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); 69849662byte[] 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;}}
- 运行效果如下: