zoukankan      html  css  js  c++  java
  • 文件重命名

    文件重命名

    // --知识点
    // --isFile():判断是否文件,也许可能是文件或者目录
    // --exists():判断是否存在,可能不存在
    // --new File(path):创建文件夹
    // --java中的isDirectory()是检查一个对象是否是文件夹。返回值是boolean类型的。如果是则返回true,否则返回false。
    // --调用方法为:对象.isDirectory() 无需指定参数。
    // --file.getAbsolutePath():获取文件的绝对路径
    // --file.getParent():文件父级路径
    // --File.separator:拼接符号
    // --path.substring(path.la stIndexOf(".")):文件后缀

    功能实现代码

    import java.io.File;
    
    /**
     * @author 马家立
     * @version 创建时间:2019年12月7日上午9:48:37
     * @Description:TODO 文件重命名
     */
    public class FileRenameDemo {
        /**
         * @Title:fileListRename
         * @author:马家立
         * @date:2019年12月7日 上午10:42:42
         * @Description:TODO 多个文件重命名
         * @param path--文件路径
         * @param number--重命名起始数
         *            void
         */
        public static void fileListRename(String path, int number) {
            // 新的文件名
            String fileNewName = "";
            // 每个重命名文件是否成功标识
            boolean result = false;
            // 根据文件路径创建文件夹
            File folder = new File(path);
            if (folder.exists()) {// 路径是否存在
                // 获取文件夹里面的所有的文件List
                File[] files = folder.listFiles();
                if ((null == files) || (0 == files.length)) {
                    System.out.println("文件夹是空的!");
                } else {
                    // 遍历所有文件
                    for (File file : files) {
                        // 判断是否是文件目录
                        if (file.isDirectory()) {
                            // 回调该方法
                            fileListRename(file.getAbsolutePath(), number);
                        } else {
                            // 获取文件的绝对路径
                            String filePath = file.getAbsolutePath();
                            // 非图片类型文件跳过
                            if (!".jpg".equals(filePath.substring(filePath.lastIndexOf(".")))
                                && !".png".equals(filePath.substring(filePath.lastIndexOf(".")))) {
                                continue;
                            }
                            if (number < 10) {
                                fileNewName = "00" + number;
                            } else if (number < 100) {
                                fileNewName = "0" + number;
                            } else {
                                fileNewName = number + "";
                            }
                            result = fileRename(filePath, fileNewName);
                        }
                        if (result) {
                            number++;
                        }
                    }
                }
            } else {
                System.out.println("该路径不存在");
            }
    
        }
    
        /**
         * @Title:fileRename
         * @author:马家立
         * @date:2019年12月7日 上午10:17:03
         * @Description:TODO 单个文件重命名
         * @param path--文件路径
         * @param newname--文件新名字
         * @return boolean
         */
        public static boolean fileRename(String path, String newname) {
            // 绝对路径拼接新名字
            String absolutePathFileName = "";
            // 创建文件
            File file = new File(path);
            // 文件是否存在
            if (file.exists()) {
                // 创建新名字的抽象文件
                absolutePathFileName = file.getParent() + File.separator + newname + path.substring(path.lastIndexOf("."));
                System.out.println("文件父级路径:" + file.getParent());
                System.out.println("拼接符号 :" + File.separator);
                System.out.println("文件后缀:" + path.substring(path.lastIndexOf(".")));
                // 重命名
                if (file.renameTo(new File(absolutePathFileName))) {
                    System.out.println("重命名成功!");
                    return true;
                } else {
                    System.out.println("重命名失败!");
                    return false;
                }
            } else {
                System.out.println("重命名文件不存在!");
                return false;
            }
        }
    
        public static void main(String[] args) {
            fileListRename("D:\mjtabu\photo\搞怪", 1);
        }
    
    }
  • 相关阅读:
    怎样重定向404页面?404页面重定向 PHP如何使404页面重定向
    css选择器(1)——元素选择器、类名和id选择器
    style对象的cssText方法
    css选择器(2)——属性选择器和基于元素结构关系的选择器
    js的闭包中关于执行环境和作用链的理解
    attachEvent和addEventListener
    使用js获取页面的各种高度
    课堂知识
    分析一个文本文件(英文文章)中各个词出现的频率,并且把频率最高的10个词打印出来。
    每日心情
  • 原文地址:https://www.cnblogs.com/mjtabu/p/12000958.html
Copyright © 2011-2022 走看看