java中对文件和目录的操作

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

public class FileUtil {
//write into file
    public static Boolean copyFile(File ofile,String newPath) throws IOException{
        FileInputStream in=new FileInputStream(ofile);
        File file=new File(newPath);
        if(file.exists()){
           return false;
        }
        if(!file.getParentFile().exists()){
            file.getParentFile().mkdirs();
        }
        file.createNewFile();
        FileOutputStream out=new FileOutputStream(file);
        int c;
        byte buffer[]=new byte[1024];
        while((c=in.read(buffer))!=-1){
            for(int i=0;i<c;i++)
                out.write(buffer[i]);
        }
        in.close();
        out.close();
        return true;
    }
//create dir
    public static Boolean createDir(String path){
        File dir=new File(path);
        if(dir.exists()){
           return false;
        }
        return dir.mkdirs();
    }
//delete file
    public static Boolean delFile(String path,String filename){
        File file;
        if (path.endsWith(File.separator)) {
            file = new File(path+filename);
        } else {
            file = new File(path+File.separator+filename);
        }
        if(file.exists() && file.isFile() && !file.canExecute()){
            return file.delete();
        }else{
            return false;
        }
    }

// delete dir and files in dir
    public static Boolean delDir(String dirPath) {
        try {
            delAllFiles(dirPath);
            File myFilePath = new File(dirPath);
            return  myFilePath.delete();

        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
// delete all file in dir
    public static boolean delAllFiles(String path) {
        System.out.println("path:"+path);
        boolean flag = false;
        File file = new File(path);
        if (!file.exists()) {
            return flag;
        }
        if (!file.isDirectory()) {
            return flag;
        }
        File[] files = file.listFiles();
        for (File f : files) {
            if (f.isFile()) {
                f.delete();
            }
            if (f.isDirectory()) {
                if (path.endsWith(File.separator)) {
                    delAllFiles(path  + f.getName());
                    delDir(path  + f.getName());
                } else {
                    delAllFiles(path + File.separator + f.getName());
                    delDir(path + File.separator + f.getName());
                }
                flag = true;
            }
        }
        return flag;
    }

//only child dir in this dir
    public static Boolean isOnlyChildDir(String path){
        File dir=new File(path);
        if(dir.exists() && dir.isDirectory() && dir.canExecute()){
            File[] files=dir.listFiles();
            if(files != null && files.length > 1){
                return false;
            }else{
                return true;
            }
        }else{
            return false;
        }
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值