- package it.cast1;
- import java.io.BufferedInputStream;
- import java.io.BufferedOutputStream;
- import java.io.FileInputStream;
- import java.io.FileOutputStream;
- import java.io.IOException;
- public class CopyFileDemo1 {
- public static void main(String[] args) throws IOException {
- // 创建文件输入流和文件输出流
- FileInputStream stream = new FileInputStream("F:\\JAVA521\\Day018\\src\\it\\cast1\\陈百强-一生何求.mp3");
- FileOutputStream stream2 = new FileOutputStream("E:\\陈百强-一生何求.mp3");
- long start = System.currentTimeMillis();
- methed(stream, stream2);//耗时62秒
- methed1(stream, stream2);//0秒
- methed2(stream, stream2);//0秒
- methed3(stream, stream2);//0秒
- long end = System.currentTimeMillis();
- System.out.println("总耗时"+(end-start)/1000+"秒");
- }
- //高效字节流数字符数组读取
- private static void methed3(FileInputStream stream, FileOutputStream stream2) throws IOException {
- //高效字节流对象创建
- BufferedInputStream bis=new BufferedInputStream(stream);
- BufferedOutputStream bos= new BufferedOutputStream(stream2);
- //新建数组放读取字节
- byte [] arr = new byte [1024];
- //读取文件放入字节数组
- int read = 0;
- while ((read=bis.read(arr))!=-1) {
- //把字节数组写入复制的文件中
- bos.write(arr, 0, read);
- }
- //关闭高效字节流
- bis.close();
- bos.close();
- }
- //字节数组读取复制文件
- private static void methed2(FileInputStream stream, FileOutputStream stream2) throws IOException {
- // 新建数组
- int read = 0;
- byte [] arr = new byte [1024];
- //读取文件并把文件中每个字符存入数字
- while ((read=stream.read(arr))!=-1) {
- //把字节数组写入复制的文件中
- stream2.write(arr, 0, read);
- }
- //关闭数据流
- stream.close();
- stream2.close();
- }
- //高效字節流复制文件
- private static void methed1(FileInputStream stream, FileOutputStream stream2) throws IOException {
- //创建高效字节输出流和输入流
- BufferedInputStream bis=new BufferedInputStream(stream);
- BufferedOutputStream bos= new BufferedOutputStream(stream2);
- //读取
- int read = 0;
- while ((read = bis.read())!=-1) {
- //写入复制
- bos.write(read);
- }
- //关闭字节流
- bis.close();
- bos.close();
-
- }
- //字节流读取复制
- public static void methed(FileInputStream stream, FileOutputStream stream2)
- throws IOException {
- // 读取文件
- int read = 0;
- while ((read = stream.read()) != -1) {
- // 写去文件
- stream2.write(read);
- }
- // 关闭数据流
- stream.close();
- stream2.close();
- }
- }
复制代码 |