下面这个是简单的将G盘根目录下的 ipv6.pptx 文本文件复制到 H 盘根目录下
只不过是一个字符 一个字符的去复制的,很慢,效率很低,现在一般不采用着用方法了。
聊为记录,以备后用。
复制到 H:\\ipv6.pptx 数据目的
字节输入流,绑定数据源
字节输出流,绑定数据目的
输入,读取1个字节
输出,写1个字节
package cn.itcast.copy;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class Copy {
public static void main(String[] args) {
//定义两个流的对象变量
FileInputStream fis = null;
FileOutputStream fos = null;
try{
//建立两个流的对象,绑定数据源和数据目的
fis = new FileInputStream("g:\\ipv6.pptx");
fos = new FileOutputStream("h:\\ipv6.pptx");
//字节数据流,读取1个字节,输出流写1个字节
int len = 0;
while((len = fis.read()) != -1){
fos.write(len);
}
}catch(IOException ex){
System.out.println(ex);
throw new RuntimeException("文件复制失败!");
}finally{
try{
if(fos != null)
fos.close();
}catch(IOException ex){
throw new RuntimeException("释放资源失败!");
}finally{
try{
if(fis != null)
fis.close();
}catch(IOException ex){
throw new RuntimeException("释放资源失败!");
}
}
}
}
}