java流

java流

字节处理流:OutputStream(输出字节流) InputStream(输入字节流)

字符处理流:Writer(输出字符流) Reader(输入字符流)

关闭流

在使用OutputStream的时候如果没有使用close()方法关闭流,内容依然可以正常输出;

在使用Writer的时候如果没有使用close()方法关闭流,内容不能正常输出,因为Writer使用到了缓冲区;flush()方法用于刷新此Writer流。 当流将来自不同write()方法的任何字符保存到缓冲区中后,立即将它们写入其预期的目标,然后在检查该目标是否包含另一个字符或字节流后,将其刷新,并且一个flush()调用将刷新所有缓冲区中的数据。

jdk1.9后可以使用transferTo(OutputStream out)进行文件拷贝。

代码示例

看懂示例代码java流理解大半

package com.junfeng.file;

import java.io.*;

/**
 * Created with IDEA
 * author:TangJunfeng
 * description:
 * Date:6/28/21 23:13
 */
public class FileUtil {

    public static void main(String[] args) throws Exception {
        String src = "/Users/junfeng/Downloads/";
        String des = "/Users/junfeng/Downloads/2/";
        FileUtil fu = new FileUtil(src, des);
        if (new File(src).isDirectory()) {
            fu.copyDir();
        } else {
            fu.copy();
        }
    }

    private File srcFile;
    private File desFile;

    public FileUtil(String src, String des) {
        this(new File(src), new File(des));
    }

    public FileUtil(File srcFile, File desFile) {
        this.srcFile = srcFile;
        this.desFile = desFile;
    }

    public boolean copyDir() throws Exception {
        try {
            this.copyImpl(this.srcFile);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean copyImpl(File file) throws Exception {
        if (file.isDirectory()) {
            File results[] = file.listFiles();
            if (results != null) {
                for (int i = 0; i < results.length; i++) {
                    copyImpl(results[i]);
                }
            }
        } else {
            String newFilePath = file.getPath().replace(this.srcFile.getPath() + File.separator, "");
            File newFile = new File(this.desFile, newFilePath);
            copyFileImpl(file, newFile);
        }
        return false;
    }

    private boolean copyFileImpl(File srcFile, File desFile) throws Exception {
        if (!desFile.getParentFile().exists()) {
            desFile.getParentFile().mkdirs();
        }

        byte data[] = new byte[1024];
        InputStream input = null;
        OutputStream ouput = null;
        try {
            input = new FileInputStream(srcFile);
            ouput = new FileOutputStream(desFile);
            int len = 0;
            while ((len = input.read(data)) != -1) {
                ouput.write(data, 0, len);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (input != null) {
                input.close();
            }
            if (ouput != null) {
                ouput.close();
            }
        }
        return false;
    }

    public boolean copy() throws Exception {
        if (!srcFile.exists()) {
            return false;
        }
        return copyFileImpl(this.srcFile, this.desFile);
    }


}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值