自己写的一个java文件操作库

 

package com.test;

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

/**
 * 通过一定的逻辑判断具体调用哪个方法
 * @author Dawn
 *
 */
public class CopyFile {
    /**
     * 在同目录下拷贝文件,加上文件名的命名逻辑
     * @param srcFilePath        源文件路径
     * @param destFilePath    目标文件路径
     */
    public static void copyFileSamePath(String srcFilePath,String destFilePath){
        File srcFile=new File(srcFilePath);
        File destFile=new File(destFilePath);
        if(!srcFile.exists()){
            System.out.println("源文件不存在");
            return;
        }
        if(!(srcFile.getPath().equals(destFile.getPath()))){
            System.out.println("方法调用错误:只复制同一个目录下的文件");
            return;
        }
        if(!srcFile.isFile()){
            System.out.println("方法调用错误:源文件不是单个的文件");
            return;
        }
        File newFile=naming(destFile);
        try{
            newFile.createNewFile();
        }catch(IOException e){
            e.printStackTrace();
        }
        copyData(srcFile,newFile);
    }
    /**
     * 用来给同目录下的新建的文件和文件夹命名
     * @param destFile        目标File类的引用
     * @return                    返回更名后的File类的引用
     */
    private static File naming(File destFile){
        File newFile=null;
        int increasing=2;
        String folder=destFile.getParent();
        String fileName="复件"+destFile.getName();
        String newPath=folder+File.separator+fileName;
        newFile=new File(newPath);
        while(newFile.exists()){
            fileName="复件"+increasing++ +" "+destFile.getName();
            newPath=folder+File.separator+fileName;
            newFile=new File(newPath);
        }
        return newFile;
    }
    /**
     * 用来传输数据
     * @param srcFile        源文件的File类引用
     * @param destFile    目标文件的File类引用
     */
    private static void copyData(File srcFile,File destFile){
        try{
            FileInputStream reader=new FileInputStream(srcFile);
            FileOutputStream writer=new FileOutputStream(destFile);
            int length=0;
            byte[] dataBytes=new byte[4096];
            while((length=reader.read(dataBytes))!=-1){
                writer.write(dataBytes,0,length);
            }
            reader.close();
            writer.close();
        }catch(FileNotFoundException e){
            e.printStackTrace();
        }catch(IOException e){
            e.printStackTrace();
        }
    }
    /**
     * 在不同目录下拷贝文件
     * @param srcFilePath        源文件路径    
     * @param destFilePath    目标文件路径    
     * @param overlay            是否覆盖
     */
    public static void copyFileDifferentPath(String srcFilePath,String destFilePath,boolean overlay){
        File srcFile=new File(srcFilePath);
        File destFile=new File(destFilePath);
        if(!srcFile.exists()){
            System.out.println("源文件不存在");
            return;
        }
        if(srcFile.getPath().equals(destFile.getPath())){
            System.out.println("方法调用错误:只能复制到不同的目录下");
            return;
        }
        if(!srcFile.isFile()){
            System.out.println("方法调用错误:只能复制文件");
            return;
        }
        if(!(srcFile.getName().equals(destFile.getName()))){
            System.out.println("输入错误:文件名不一样");
            return;
        }
        if(destFile.exists()){
            if(overlay){
                destFile.delete();
            }else{
                System.out.println("文件重名");
                return;
            }
        }
        try{
            destFile.createNewFile();
        }catch(IOException e){
            e.printStackTrace();
            return;
        }
        copyData(srcFile,destFile);
    }
    /**
     * 在不同的目录下拷贝文件夹
     * @param srcFolderPath        源文件夹路径
     * @param destFolderPath    目标文件夹路径    
     * @param overlay                是否覆盖
     */
    public static void copyFolderDifferentPath(String srcFolderPath,String destFolderPath,boolean overlay){
        File srcFolder=new File(srcFolderPath);
        File destFolder=new File(destFolderPath);
        if(!srcFolder.exists()){
            System.out.println("源文件不存在");
            return;
        }
        if(srcFolder.getPath().equals(destFolder.getPath())){
            System.out.println("方法调用错误:只能复制到不同的目录下");
            return;
        }
        if(!srcFolder.isDirectory()){
            System.out.println("方法调用错误:只能复制文件夹");
            return;
        }
        if(!(srcFolder.getName().equals(destFolder.getName()))){
            System.out.println("输入错误:文件名不一样");
            return;
        }
        if(destFolder.exists()){
            if(overlay){
                File[] files=srcFolder.listFiles();
                int size=files.length;
                for(int i=0;i<size;i++){
                    if(files[i].isFile()){
                        copyFileDifferentPath(files[i].getPath(),destFolder.getPath()+File.separator+files[i].getName(),true);
                    }else{
                        copyFolderDifferentPath(files[i].getPath(),destFolder.getPath()+File.separator+files[i].getName(),true);
                    }
                }
            }else{
                System.out.println("文件夹重名");
                return;
            }
        }else{
            destFolder.mkdirs();
            copyFolderDifferentPath(srcFolderPath,destFolderPath,true);
        }
    }
    public static void copyFolderSamePath(String srcFolderPath,String destFolderPath){
        File srcFolder=new File(srcFolderPath);
        File destFolder=new File(destFolderPath);
        if(!srcFolder.exists()){
            System.out.println("源文件夹不存在");
            return;
        }
        if(!(srcFolder.getPath().equals(destFolder.getPath()))){
            System.out.println("方法调用错误:只复制同一个目录下的文件夹");
            return;
        }
        if(!srcFolder.isDirectory()){
            System.out.println("方法调用错误:源文件不是单个的文件夹");
            return;
        }
        File newFolder=naming(destFolder);
        newFolder.mkdirs();
        File[] files=srcFolder.listFiles();
        int size=files.length;
        for(int i=0;i<size;i++){
            if(files[i].isFile()){
                copyFileDifferentPath(files[i].getPath(),newFolder.getPath()+File.separator+files[i].getName(),false);
            }else{
                copyFolderDifferentPath(files[i].getPath(),newFolder.getPath()+File.separator+files[i].getName(),false);
            }
        }
        
    }
    /**
     * 测试用main方法
     * @param args
     */
    public static void main(String[] args){
        copyFileSamePath("F:\\World of  Warcraft\\Data\\data\\data.019","F:\\World of  Warcraft\\Data\\data\\data.019");
    }
}




//删除功能,配合上面的CopyFile可以达到剪切效果
package folderoperation;

import java.io.File;
/**
 * 注意它会删除文件,文件夹以及文件夹下的所有内容(根据指定的地址)
 * @author Dawn
 *
 */
public class DeleteFolder {
/**
 * 删除文件的方法
 * @param filePath 文件或文件夹的地址
 * @return
 */
    public static boolean delete(String filePath){
        File file=new File(filePath);
        if(file.exists()){
            if(file.isFile()){
                file.delete();
                return true;
            }else{
                File[] files=file.listFiles();
                int size=files.length;
                for(int i=0;i<size;i++){
                    DeleteFolder.delete(files[i].getPath());
                }
                file.delete();
                return true;
            }
        }else{
            System.out.println("文件或文件夹不存在");
            return false;
        }
    }
    public static void main(String[] args){
        delete("/Users/Dawn/Documents/JavaPractice/dataFile.txt");
    }
}

 

转载于:https://www.cnblogs.com/xzxdm/p/5667604.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值