标签:result tco mil nal pack inpu cal 字节 finally
package A10_IOStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileOutputStream;
import java.math.BigDecimal;
import java.math.RoundingMode;
public class D03_FileCopy {
public static void main(String[] args) throws IOException {
myCopyFile("D:\\视频教程\\从零开始学asp.netCore\\1.2为您的机器配置开发环境.wmv", "D:\\配置开发环境.wmv");
}
/*
使用字节流复制文件
四舍五入:
Double result=new BigDecimal(3.1415926).setScale(2, RoundingMode.HALF_UP).doubleValue();
*/
private static void myCopyFile(String s, String d) throws IOException {
long timeB = System.currentTimeMillis(); //复制开始毫秒时间
byte[] buff = new byte[1024 * 1024]; //缓冲区大小1024KB
FileInputStream fis = new FileInputStream(s); //创建输入流对象(输入到内存)
FileOutputStream fos = new FileOutputStream(d); //创建输出流对象(从内存输出到硬盘文件)
try {
int len = 0; //读取有效字节数
while ((len = fis.read(buff)) != -1) { //读取有效字节非-1值循环读写
fos.write(buff, 0, len); //从内存写出到文件
}
} finally {
fis.close();
fos.close();
}
long timeE=System.currentTimeMillis();
Double duration=Math.round(timeE-timeB)*0.001; //转换为秒的数值
duration=new BigDecimal(duration).setScale(4, RoundingMode.HALF_UP).doubleValue(); //经典的中国四舍五入
System.out.println("文件复制耗时:"+duration+"秒");
}
}
标签:result tco mil nal pack inpu cal 字节 finally
原文地址:https://www.cnblogs.com/sdlz/p/12771738.html