文件复制

复制的核心:读写字节

同样可以用去上传,下载文件–及互联网的数据传输

%title插图%num

使用String为参数的方法来复制会导致一个问题:

效率特别的低。

String为参数的方法的代码:

package cn.hiluna.demo;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 将数据源e:\\a.txt
 * 复制到d:\\a.txt 数据目的
 * 字节输入流,绑定数据源
 * 字节输出流,绑定数据目的
 *
 * 输入,读取1个字节
 * 输出,写1个字节
 */
public class copy {
    public static void main(String[] args) {
        //定义2个流对象
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;

        try {
            //建立2个流对象,绑定数据源和目的源
            fileInputStream = new FileInputStream("e:\\a.txt");
            fileOutputStream = new FileOutputStream("d:\\a.txt");

            //字节输入流,读取1个字节,输出一个字节
            int len = 0;
            while ((len = fileInputStream.read()) != -1){
                fileOutputStream.write(len);
            }
        }catch (IOException ex){
            System.out.println(ex);
            throw new RuntimeException("文件复制失败");
        }finally {
            try {
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            }catch (IOException ex){
                throw new RuntimeException("释放资源失败");
            }finally {
                try {
                    if (fileInputStream != null){
                        fileInputStream.close();
                    }
                }catch (IOException ex){
                    throw new RuntimeException("释放资源失败");
                }
            }
        }
    }
}

因此需要选择一数组为参数的方法来进行复制

以下为代码:

package cn.hiluna.demo;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 字节流复制文件
 * 采用数组的缓冲提高效率
 * 字节数组
 * FileInputStream 读取字节数组
 * FileOutStream 写字节数组
 */
public class copy2 {
    public static void main(String[] args) {
        long s = System.currentTimeMillis();
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream("e:\\0day2.pdf");
            fileOutputStream = new FileOutputStream("d:\\0day2.pdf");

            //定义一个字节数组,用于实现缓冲
            byte[] bytes = new byte[1024];
            //读取数组写入数组
            int len = 0;
            while ((len = fileInputStream.read(bytes)) != -1) {
                fileOutputStream.write(bytes, 0, len);
            }

        } catch (IOException ex) {
            System.out.println(ex);
            throw new RuntimeException("复制失败");
        } finally {
            try {
                if (fileOutputStream != null) {
                    fileOutputStream.close();
                }
            } catch (IOException ex) {
                throw new RuntimeException("释放资源失败");
            } finally {
                try {
                    if (fileInputStream != null) {
                        fileInputStream.close();
                    }
                } catch (IOException ex) {
                    throw new RuntimeException("释放资源失败");
                }
            }
        }
        long e = System.currentTimeMillis();
        System.out.println(e - s);
    }
}

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇