此时情绪此时天,无事小神仙
好好生活,平平淡淡每一天

编辑

DocumentOperations

读取文件内容
public void fileRead() throws Exception {
        File file = new File("E:\\test\\test.txt");//定义一个file对象,用来初始化FileReader
        FileReader reader = new FileReader(file);//定义一个fileReader对象,用来初始化BufferedReader
        BufferedReader bReader = new BufferedReader(reader);//new一个BufferedReader对象,将文件内容读取到缓存
        StringBuilder sb = new StringBuilder();//定义一个字符串缓存,将字符串存放缓存中
        String s = "";
        while ((s =bReader.readLine()) != null) {//逐行读取文件内容,不读取换行符和末尾的空格
            sb.append(s + "\n");//将读取的字符串添加换行符后累加存放在缓存中
           System.out.println(s);
        }
        bReader.close();
        String str = sb.toString();
        System.out.println(str );
        //return str;
  }
文件重命名

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);
    }

}
posted @ 2021-05-12 17:39  踏步  阅读(1145)  评论(0编辑  收藏  举报