package com.apesblog.day_26;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test6 {
public static void main(String[] args) {
long start = System.currentTimeMillis();
bufferTest("src/com/apesblog/day_26/01.zip", "src/com/apesblog/day_26/buffercopy.zip");
long end = System.currentTimeMillis();
System.out.println(end - start);
start = System.currentTimeMillis();
fileTest("src/com/apesblog/day_26/01.zip", "src/com/apesblog/day_26/filecopy.zip");
end = System.currentTimeMillis();
System.out.println(end - start);
}
public static void bufferTest(String sfile, String scopy) {
BufferedInputStream bufferInputStream = null;
BufferedOutputStream bufferedOutputStream = null;
try {
byte[] buffer = new byte[1024];
int len;
bufferInputStream = new BufferedInputStream(new FileInputStream(new File(sfile)));
bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(scopy)));
while ((len = bufferInputStream.read(buffer)) != -1) {
bufferedOutputStream.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bufferInputStream != null)
bufferInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (bufferedOutputStream != null)
bufferedOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void fileTest(String sfile, String scopy) {
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
byte[] buffer = new byte[1024];
int len;
fileInputStream = new FileInputStream(sfile);
fileOutputStream = new FileOutputStream(scopy);
while ((len = fileInputStream.read(buffer)) != -1) {
fileOutputStream.write(buffer, 0, len);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (fileInputStream != null)
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
try {
if (fileOutputStream != null)
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
版权归属:
小顺
许可协议:
本文使用《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》协议授权
评论区