/*
使用字节流将D盘视频文件传到C盘other文件下
*/
public class Class {
public static void main(String[] args) throws IOException {
//第一步 读取D盘里的视频文件
String path = "D:\\A\\B\\me.mp4";
File file = new File(path);
/*
构造方法摘要
FileInputStream(File file)
通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的 File 对象 file 指定。
FileInputStream(String name)
通过打开一个到实际文件的连接来创建一个 FileInputStream,该文件通过文件系统中的路径名 name 指定。
*/
FileInputStream fileInputStream = new FileInputStream(file);
/*
构造方法摘要
BufferedInputStream(InputStream in)
创建一个 BufferedInputStream 并保存其参数,即输入流 in,以便将来使用。
*/
String lastPath = "C:\\Other\\me.mp4";
BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(lastPath));
//读取该路径的文件。
/*
read()
从此输入流中读取一个数据字节。
int read(byte[] b)
从此输入流中将最多 b.length 个字节的数据读入一个 byte 数组中。
int read(byte[] b, int off, int len)
从此输入流中将最多 len 个字节的数据读入一个 byte 数组中
*/
/*
规定一次读取1024个byte
*/
byte b [] = new byte[1024];
int len;
//无参的read()方法返回的int类型,是表示数据下一个字节的字节码
while ((len=fileInputStream.read(b))!=-1)
{
//把读取的文件写入C盘other文件下
// void write(byte[] b, int off, int len)
// 将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此缓冲的输出流。
// void write(int b)
// 将指定的字节写入此缓冲的输出流。
//这里一定要写len 不然如果有1039个 一次读1024 剩下的15个就读不出来 会造成文件损失
bufferedOutputStream.write(b,0,len);
}
System.out.println("复制成功");
//写完关闭流
bufferedInputStream.close();
bufferedOutputStream.close();
}
}
复习IO流-复制文件
最新推荐文章于 2024-07-12 16:26:30 发布