如何使用IO流移动文件

如何使用IO流移动文件

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Scanner;
public class Demo6 {
    public static void main(String[] args) {
        //IO流移动文件,不是拷贝
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入源路径");
        String oldP = scanner.nextLine();
        System.out.println("请输入目标路径");
        String newP = scanner.nextLine();
        String regex = "[a-zA-Z]:(((\\\\(?! )[^/:*?<>\\\"\"|\\\\]+)+\\\\?)|(\\\\)?)\\s*";
        if (oldP.matches(regex) && newP.matches(regex)) {
            File oldPath = new File(oldP);
            if (!oldPath.exists()){
                System.out.println("源路径不存在");
                return;
            }else if(oldPath.length()==0){
                System.out.println("源路径无内容");
                return;
            }
            File newPath = new File(newP);
            if (!newPath.exists()){
                newPath.mkdirs();
            }
            removeFile(oldPath, newPath);
            deleteFile(oldPath);
            System.out.println("移动成功");
        } else {
            System.out.println("路径错误,请检查");
        }
    }

    private static void deleteFile(File oldPath) {//删除源文件
        if (oldPath.exists()){
            File[] files = oldPath.listFiles();
            for (File file : files) {
                if(file.isFile()){
                    file.delete();
                }else {
                    deleteFile(file);
                }
            }
            boolean delete = oldPath.delete();
            System.out.println(delete);
        }
    }
    private static void removeFile(File oldPath, File newPath) {//复制
        if (oldPath.exists()) {
            File[] files = oldPath.listFiles();
            for (File file : files) {
                if (file.isFile()) {
                    copyFile1(file, newPath);
                } else {
                    File file1 = new File(newPath, file.getName());
                    file1.mkdirs();
                    removeFile(file, file1);
                }
            }
        }
    }
    private static void copyFile1(File file, File newPath) {//复制
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try {
            fileInputStream = new FileInputStream(file);
            fileOutputStream = new FileOutputStream(new File(newPath, file.getName()));
            int len = 0;
            byte[] bytes = new byte[1024 * 8];
            while ((len = fileInputStream.read(bytes)) != -1) {
                fileOutputStream.write(bytes, 0, len);
                fileOutputStream.flush();
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (fileInputStream != null) {
                   fileInputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                if (fileOutputStream != null)
                    fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值