package FileStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class CopyDemo {
public static void main(String[] args) throws Exception {
// 1 创建源,目标
File srcFile = new File("file/stream.txt");
File destFile = new File("file/stream_copy.txt");
// 2 创建输入的管道(输入流对象 / 输出流对象)
InputStream in = new FileInputStream(srcFile);
OutputStream out = new FileOutputStream(destFile);
// 3 读取 / 写出 操作
byte[] buffer = new byte[3];//创建容量为10的缓冲区,存储已经读取的字节数据
int len = -1; //表示已经读取了多少字节数,如果是-1表面文件读到最后
//len = in.read(buffer); //表面读取了多少个字节
while((len = in.read(buffer)) != -1){
System.out.println(new String(buffer, 0 ,len));
out.write(buffer, 0, len);
}
// 4 关闭管道(关闭资源)
in.close();
out.close();
}
}
文件的拷贝
最新推荐文章于 2024-07-02 22:08:50 发布