本地文件的批量创建,修改工具类

来源:互联网 发布:js添加元素到指定位置 编辑:程序博客网 时间:2024/05/04 14:05
import java.io.File;import java.io.IOException;import java.io.RandomAccessFile;public class FileUtil {/** * 对本地文件操作的类,实现文件复制,批量创建文件,批量修改文件名称 * @param file 源文件的对象,文件绝对路径 * @param path 要复制的目标路径,只需要目录层级 * @throws IOException 文件传输异常 * @author ytf.site */    public void copyFile(File file,String path) throws IOException{        //1,把源文件读取到内存中        RandomAccessFile raf=new RandomAccessFile(file, "rw");        int i=(int) file.length();        byte[] b=new byte[i];        raf.read(b);        //2,判断目标路径是否存在,不存在创建该路径        File mbPath=new File(path);        if(!mbPath.exists()){            mbPath.mkdirs();        }        // 3.生成复制后的文件名,并创建复制后的文件        String name=file.getName();        String old=name.substring(0,name.lastIndexOf("."));        String newName=name.replace(old, old+"_副本");//替换源文件名中.前边的一部分        File newFile=new File(mbPath, newName);        if(!newFile.exists()){            newFile.createNewFile();        }        // 4.向要复制的文件输出数据        RandomAccessFile out=new RandomAccessFile(newFile, "rw");        out.write(b);        //5.关闭资源流        out.close();        raf.close();    }    /** 批量创建文件     * @param file 要创建文件的父级目录     * @param fileName 要创建的文件名     * @param hz 要创建的文件后缀名     * @param num 要创建的文件数量     * @return 返回成功创建文件的数量     * @throws IOException 文件传输异常     */    public int batchCreateFiles(File file,String fileName,String hz,int num) throws IOException{        //1.判断父级目录是否存在,如果不存在,创建该目录        if(!file.exists()){            file.mkdirs();        }        //2.批量创建文件        int len=String.valueOf(num).length();//确定最大几位数        for (int i = 1; i <=num; i++) {            File f=new File(file,fileName+String.format("%0"+len+"d"+hz, i));//数字格式化            @SuppressWarnings("unused")            int count=0;            if(!f.exists()){                boolean b=f.createNewFile();                if(b){                    count++;//记录成功创建文件的个数                }            }        }        System.out.println("成功创建"+num+"个文件");        return num;    }    /**     * 批量修改文件     * @param file 要修改文件的父级目录     * @param oldName 需要修改的文件名     * @param newName 修改过后的文件名     * @return 返回成功修改文件的个数,     * @return 若返回-1,说明父级目录不存在,已经结束该方法的使用     */    public int renameFiles(File file,String oldName,String newName){        // 1.判断文件所在的父级目录是否存在        if(!file.exists()){            System.out.println("此目录不存在");            return -1;//如果文件父级目录有误,跳出该方法,结束修改        }        //2.获得该父级目录下的所有文件        File[] files=file.listFiles();        int count=0;        for (File f : files) {            String name=f.getName();//获取文件带后缀的全名字            String name2=name.replace(oldName, newName);            //获取修改后的名字(替换老名字中的部分字段),得到新名字的绝对路径            File newFile=new File(file,name2);            boolean b=f.renameTo(newFile);//彻底将源文件名字改为修改后的            if(b){                count++;            }else{                System.out.println("修改失败!");            }        }        System.out.println("成功修改了"+count+"个文件");        return count;    }}
0 0
原创粉丝点击