package io;
import org.junit.Test;
import java.io.*;
public class BufferedStreamDemoTest {
private String path0 = System.getProperty("user.dir") + "/src/main/resources/copy0.txt";
private String path = System.getProperty("user.dir") + "/src/main/resources/d.txt";
private File file = null;
private BufferedInputStream bis = null;
private BufferedOutputStream bos = null;
@Test
public void copyTxt() throws Exception{
file = new File(path0);
bis = new BufferedInputStream(new FileInputStream("C:\\Users\\15082157\\Desktop\\自我介绍.txt"));
bos = new BufferedOutputStream(new FileOutputStream(file));
if (!(file.exists())) {
file.createNewFile();
BufferedStreamCopy();
} else {
BufferedStreamCopy();
}
}
@Test
public void copyMp4() throws Exception {
String path1 = System.getProperty("user.dir") + "/src/main/resources/copy.mp4";
file = new File(path1);
bis = new BufferedInputStream(new FileInputStream("C:\\Users\\15082157\\Desktop\\3-4视频19秒.mp4"));
bos = new BufferedOutputStream(new FileOutputStream(file));
if (!(file.exists())) {
file.createNewFile();
BufferedStreamCopy();
} else {
BufferedStreamCopy();
}
}
public void BufferedStreamCopy() throws Exception {
byte[] buffer = new byte[1024]; // 创建一个长度是1024的byte类型的数组,用于存储读取到的数据
int byteRead = 0;
long beginTime = getCurrentTime1();
while ((byteRead = bis.read(buffer)) != -1) {
bos.write(buffer, 0, byteRead);
bos.flush();
}
long endTime = getCurrentTime1();
float time = (endTime - beginTime) / 1000F;
System.out.println("copy时间为" + time + "s");
bis.close();
bos.close();
}
public long getCurrentTime1() {
long currentTime = System.currentTimeMillis();
System.out.println(currentTime);
return currentTime;
}
}