1.用字节流复制文件,代码如下:
/*
* 用字节流复制文本文件。
* 把项目路径下的FileOutputStreamDemo.java复制到d:\\Copy.java中。
*
* 数据源:
* FileOutputStreamDemo.java -- 读取数据 -- FileInputStream
* 目的地:
* FileOutputStreamDemo.java -- 写入数据 -- FileOutputStream
*/
public class CopyFile {
public static void main(String[] args) throws IOException {
// 封装数据源
FileInputStream fis = new FileInputStream("FileOutputStreamDemo.java");
// 封装目的地
FileOutputStream fos = new FileOutputStream("d:\\Copy.java");
// 基本读写操作
// 方式1
// int by = 0;
// while((by=fis.read())!=-1){
// fos.write(by);
// }
// 方式2
byte[] bys = new byte[1024];
int len = 0;
while((len=fis.read(bys))!=-1){
fos.write(bys,0,len);
}
// 释放资源
fos.close();
fis.close();
}
} |
|