java-study使用FileInputStream与FileOutputStream文件读/写/复制/递归拷贝,枚举的使用

import java.io.FileInputStream;
import java.io.FileOutputStream;
//import org.apache.commons.lang.CharEncoding;

/**
 * file读写工具类
 */
public class FileReadAndWrite {
    public static void main(String[] args) {
//        FileReadAndWrite.read("E:\\java-study\\src\\com\\streamTest\\fis2.txt");
//        FileReadAndWrite.write("E:\\java-study\\src\\com\\streamTest\\fos.txt", "linge加油", WriteType.ADD);
        FileReadAndWrite.copy("C:\\Users\\lucklin\\Desktop\\abc.png", "E:\\java-study\\src\\com\\streamTest\\abc.png");
        File source = new File("..\\java_file");
        File target = new File("..\\Desktop");
        filesCopy(source, target);
    }
    /**
     * @description 读取文件内容
     * @param path 文件路径
     */
    static String read(String path) {
        String res = null;
        try {
            FileInputStream fis = new FileInputStream(path);
            byte[] b = new byte[1024];
            int len = 0;

            while ((len = fis.read(b)) != -1) {
                res = new String(b,0, len);
            }
            fis.close();
        } catch(Exception e) {
            System.out.println("文件读取异常:"+e.getMessage());
        }
        return res;
    }
    /**
     * @description 写入文件内容
     * @param path 文件路径
     * @param content 写入内容
     */
    static void write(String path, String content) {
        try {
            //new FileOutputStream在文件不存在的情况下会默认创建,所以虽然会抛出不存在异常但是不会阻塞后续执行,前提path要正确
            FileOutputStream fos = new FileOutputStream(path);
            byte[] b = content.getBytes();
//            System.out.println(b.length);
//            fos.write(b,0, b.length);//写入内存
            fos.write(b);//写入内存
            fos.flush();//写入硬盘
            fos.close();
            System.out.println("写入成功");
        } catch (Exception e) {
            System.out.println("文件写入异常:"+e.getMessage());
        }
    }
    /**
     * @description 写入文件内容
     * @param path 文件路径
     * @param content 写入内容
     * @param type 写入方式
     */
    static void write(String path, String content, WriteType type) {
        String oldContent = read(path);
        content = oldContent != null && type.equals(WriteType.ADD) ? oldContent + content : content;
        write(path, content);
    }
    /**
     * @description 复制文件
     * @param source 文件路径
     * @param target 目标文件路径
     */
//    static void copy(String source, String target) {//new String()和String.getBytes的编码问题有时间再研究
//        String content = read(source);
//        if(content != null) write(target, content);
//    }
    static void copy(String source, String target) {
       try {
           FileInputStream fis = new FileInputStream(source);
           FileOutputStream fos = new FileOutputStream(target);
           byte[] b = new byte[1024];
           int len = 0;

           while ((len = fis.read(b)) != -1) {
               fos.write(b, 0, len);
           }
           fos.flush();
           fis.close();
           fos.close();
       }catch (Exception e) {
           e.printStackTrace();
       }
    }

    /**
     * @description 递归拷贝文件及目录
     * @param source 文件路径
     * @param target 目标文件路径
     */
    static void filesCopy(File source, File target) {
        if(source.isFile()) {
            FileInputStream in = null;
            FileOutputStream out = null;
            try {
                in = new FileInputStream(source);
                String path = target.getAbsolutePath() +  source.getAbsolutePath().substring(source.getAbsolutePath().indexOf("\\java_file"));
                out = new FileOutputStream(path);
                byte[] temp = new byte[1024 * 1024];
                int num = 0;
                while ((num = in.read(temp)) > -1) {
                    out.write(temp, 0, num);
                }
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if(in != null) in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                try {
                    if(out != null) out.flush();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }else{
            for (File f : source.listFiles()) {
                if(f.isDirectory()) {
                    String f_path = f.getAbsolutePath();
                    String dist_path = target.getAbsolutePath() +  f_path.substring(f_path.indexOf("\\java_file"));
                    File new_file = new File(dist_path);
                    if(!new_file.exists()) new_file.mkdirs();
                }
                filesCopy(f, target);
            };
        }
    }

}

enum WriteType{
//    ADD,
//    REPLACE
    ADD("add", "追加写入"),
    REPLACE("replace", "覆盖写入");
    private final String TYPE;
    private final String DESC;
    WriteType(String type, String desc) {
        TYPE = type;
        DESC = desc;
    }
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值