package com.itheima;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test6
{
/**
* 第6题:使用带缓冲功能的字节流复制文件。
* @param args
*/
public static void main(String[] args)
{
//定义两个File对象,指向源文件及目的文件
File sourcefile = new File("C:\\Users\\Administrator\\Desktop\\exam.doc");
File purposefile = new File("C:\\Users\\Administrator\\Desktop\\exam1.doc");
//定义两个带缓冲功能的字节流
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try
{
//将文件与字节流相关联
bis = new BufferedInputStream(new FileInputStream(sourcefile));
bos = new BufferedOutputStream(new FileOutputStream(purposefile));
int b = 0;
//从源文件读取一个字节,并写入目的文件
while((b = bis.read()) != -1)
{
bos.write(b);
}
} catch (FileNotFoundException e)
{
// TODO 自动生成的 catch 块
e.printStackTrace();
}catch (IOException e1)
{
// TODO 自动生成的 catch 块
e1.printStackTrace();
}
//关闭资源
finally
{
try
{
if(bis != null)
bis.close();
} catch (IOException e2)
{
// TODO: handle exception
e2.printStackTrace();
}
try
{
if(bos != null)
bos.close();
} catch (IOException e3)
{
// TODO: handle exception
e3.printStackTrace();
}
}
}
} |
|