我们复制一下元神的安装包
package Files;
import java.io.*;
public class TestFileOutputStream {
public static void main(String[] args) {
//fileOutput();
//fileOutput();
// inputOutput();
//拷贝元神
bufferedOutput();
}
public static void bufferedOutput() {
//记录拷贝需要的时间
long start = System.currentTimeMillis();
System.out.println("开始拷贝元神");
try (BufferedOutputStream bufferedOutput = new BufferedOutputStream(new FileOutputStream("E:\\原神.exe"))) {
try (BufferedInputStream input = new BufferedInputStream(new FileInputStream("E:\\test\\元神.exe"))){
int b;
while ((b = input.read()) != -1) {
bufferedOutput.write(b);
}
}catch (IOException e) {
e.printStackTrace();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
System.out.println("时间:"+(System.currentTimeMillis() - start));
}
}
Reader 和 Writer不能复制非纯文本文件
会导致损坏
package Files;
import java.io.*;
public class TestWriter {
public static void main(String[] args) {
System.out.println("开始");
long start = System.currentTimeMillis();
try(Reader reader = new FileReader(new File("E:\\test\\元神.exe"))) {
int b ;
try(Writer writer = new FileWriter(new File("E:\\原神1.exe"))){
while ((b = reader.read()) != -1) {
writer.write(b);
}
}
catch (IOException e) {
throw new RuntimeException(e);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
System.out.println("结束00");
System.out.println(System.currentTimeMillis() - start);
}
}
评论 (0)