思路:
- 创建输入流对象
- 创建输出流对象
- 读写方法
- 释放资源
实现代码:
package File_Demo;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class File_demo10 {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
long start = System.currentTimeMillis();
// method1()
// method2();
// method3();
method4();
long end = System.currentTimeMillis();
System.out.println(end-start);
}
// public static void method1() throws IOException {
// BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\JAVA\\qq.MP4"));
// BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\JAVA\\h\\qq.MP4"));
// //一次读一个字节
// int by;
// while((by = bis.read())!=-1) {
// bos.write(by);
// }
// bis.close();
// bos.close();
// }
// public static void method2() throws IOException {
// BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\JAVA\\qq.MP4"));
// BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\JAVA\\javaSe\\qq.MP4"));
// //一次读一个字节数组
// int len;
// byte[] b = new byte[1024];
// while((len = bis.read(b))!=-1) {
// bos.write(b,0,len);
// }
// bis.close();
// bos.close();
// }
// public static void method3() throws IOException {
// FileInputStream fis = new FileInputStream("D:\\JAVA\\qq.MP4");
// FileOutputStream fos = new FileOutputStream("D:\\JAVA\\javaSe\\qq.MP4");
// //一次读一个字节
// int by;
// while((by = fis.read())!=-1) {
// fos.write(by);
// }
// fis.close();
// fos.close();
//
// }
public static void method4() throws IOException {
FileInputStream fis = new FileInputStream("D:\\JAVA\\qq.MP4");
FileOutputStream fos = new FileOutputStream("D:\\JAVA\\javaSe\\qq.MP4");
//一次读一个字节数组
int len;
byte[] b = new byte[1024];
while((len = fis.read(b))!=-1) {
fos.write(b,0,len);
}
fis.close();
fos.close();
}
}