字节流

FileInputStream和FileOutputStream的使用

一,使用字节流FileInputStream处理文本文件(可能异常)

使用同一套结构构建代码:

@Test
    public void testFileInputStream() {
        //使用字节流FileInputStream处理文本文件,可能出现乱码。
        FileInputStream fis = null;
        try {
            //1.造文件
            File file = new File("hello.txt");

            //2.造流
            fis = new FileInputStream(file);

            //3.读数据
            byte[] buffer = new byte[5];
            int len ;//记录每次读取的字节的个数
            while ((len = fis.read(buffer))!= -1){
                String str = new String(buffer,0,len);
                System.out.print(str);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (fis!=null)
            //4.关闭资源
            {
                try {
                    fis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
二,图片的复制操作
@Test
public void testFileInputOutputStream() {
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
        //
        File srcFile = new File("1.jpg");
        File destFile = new File("11.jpg");
        //
        fis = new FileInputStream(srcFile);
        fos = new FileOutputStream(destFile);

        //复制的过程(字节流)
        byte[] buffer = new byte[5];
        int len;
        while ((len = fis.read(buffer))!= -1){
            fos.write(buffer,0,len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fos!=null){
        try {

            fos.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        }
        if (fis!=null){
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
三,指定路径下文件的复制
public void copyFile(String srcPath, String destPath){
    FileInputStream fis = null;
    FileOutputStream fos = null;
    try {
        //
        File srcFile = new File(srcPath);
        File destFile = new File(destPath);
        //
        fis = new FileInputStream(srcFile);
        fos = new FileOutputStream(destFile);

        //复制的过程
        byte[] buffer = new byte[1024];
        int len;
        while ((len = fis.read(buffer))!= -1){
            fos.write(buffer,0,len);
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fos!=null){
            try {

                fos.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (fis!=null){
            try {
                fis.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

@Test
    public void testCopyFile(){
        long start = System.currentTimeMillis();
        String srcPath = "D:\\BaiduNetdiskDownload\\Anaconda3-2021.05-Linux-x86_64.sh";
        String destPath = "D:\\BaiduNetdiskDownload\\1.sh";
//        String srcPath = "hello.txt";
//        String destPath = "hello3.txt";
        copyFile(srcPath,destPath);

        long end = System.currentTimeMillis();
        System.out.println("复制操纵花费的时间为:"+(end-start));//byte[5] 时间太长 byte[1024] 2788ms
    }
四,结论
  1. 对于文本文件(.txt,.java,.c,.cpp),使用字符流处理
  2. 对于非文本文件(.jpg,.mp3,.mp4,.avi,.doc,.ppt,...),使用字节流处理
posted @ 2022-09-21 20:47  不落微笑  阅读(47)  评论(0)    收藏  举报