数据源: D:\\12.avi -- 读取数据 -- FileInputStream
目的地:copy.avi -- 写出数据 -- FileOutputStream
步骤:
1: 封装数据源
2: 封装目的地
3: 读取数据
4: 写出数据到目的地
5: 释放资源
public class CopyVideo {
public static void main(String[] args) throws IOException {
long star = System.currentTimeMillis();
method1("D:\\12.avi","copy1.avi");
method2("D:\\12.avi","copy2.avi");
method3("D:\\12.avi","copy3.avi");
method4("D:\\12.avi","copy4.avi");
long end = System.currentTimeMillis();
System.out.println("共耗时:"+(end-star)+"毫秒");
}
//1,高效的流,一次一个字节数组。
private static void method4(String srcPath, String destPath) throws IOException {
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(srcPath));
BufferedOutputStreambos=newBufferedOutputStream(newFileOutputStream(destPath));
//读取数据
byte[] buffer=new byte[1024];
int len =0;
while ((len=bis.read())!=-1) {
//写数据到目的地
bos.write(buffer,0,len); }
bis.close();
bos.close();
}
//,2,高效的流 一次一个字节。
private static void method3(String srcPath, String destPath) throws IOException {
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(srcPath));
BufferedOutputStreambos=newBufferedOutputStream(newFileOutputStream(destPath));
//读取数据
int by=0;
while ((by=bis.read())!=-1) {
//写数据到目的地
bos.write(by); }
bis.close();
bos.close();
}
/,3,基本的流 一次一个字节数组。
private static void method2(String srcPath, String destPath) throws IOException {
FileInputStream fis=new FileInputStream(srcPath);
FileOutputStream fos=new FileOutputStream(destPath);
//读取数据
byte[] buffer=new byte[1024];
int len=0;
while ((len=fis.read())!=-1) {
//写数据到目的地
fos.write(buffer,0,len); }
fis.close();
fos.close();
}
/,4,基本的流 一次一个字节。
private static void method1(String srcPath, String destPath) throws IOException {
FileInputStream fis=new FileInputStream(srcPath);
FileOutputStream fos=new FileOutputStream(destPath);
//读取数据
int by=0;
while ((by=fis.read())!=-1) {
//写数据到目的地
fos.write(by); }
fis.close();
fos.close();
}
}