当前位置: 首页 > 工具软件 > Bibi > 使用案例 >

bibi下载的视频批量重命名

凌伟泽
2023-12-01
@Test
public void testUrl() throws Exception{
    //解析url地址,第一个参数是访问的url,第二个参数是访问时候的超时时间
    Document doc = Jsoup.parse(new URL("https://coding.imooc.com/class/chapter/146.html#Anchor"),2000);
    Elements infolayout_clearfix = doc.getElementsByClass("infolayout clearfix");
    System.out.println(infolayout_clearfix.text());
}
<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-io -->
<dependency>
    <groupId>org.apache.commons</groupId>
    <artifactId>commons-io</artifactId>
    <version>1.3.2</version>
</dependency>

一、目录结构

D:\BaiduNetdiskDownload>tree /F 57779382
卷 DATA 的文件夹 PATH 列表
卷序列号为 1661-2745
D:\BAIDUNETDISKDOWNLOAD\57779382
│  57779382.dvi
│  cover.jpg
│  desktop.ini
│  慕课链接.txt
│  目录.txt

├─1
│      57779382.info
│      57779382_1.xml
│      57779382_1_0.flv

import org.apache.commons.io.FileUtils;
import org.junit.Test;
import java.io.*;
import java.util.ArrayList;

/**
 * Created by yuanyu on 2019/7/3
 */
public class FileDemo {

    private static ArrayList<String> fileNameArrayList = new ArrayList<>();
    private static ArrayList<File> fileArrayList = new ArrayList<>();
    /**bibi下载的文件夹*/
    private static String filePath = "D:/BaiduNetdiskDownload/57779382";
    //提前创建好的,文件复制到那个目录下
    private static String copyPath = "D:/BaiduNetdiskDownload/剑指Java面试-Offer直通车";


    // @Test
    public void init() throws IOException {
        /*目录文件*/
        BufferedReader bufferedReader = new BufferedReader(new FileReader(filePath + "/目录.txt"));

        String line = null;
        while ((line = bufferedReader.readLine()) != null) {
            if (!line.equals("")) {
                fileNameArrayList.add(line.trim());
            }
        }
    }


    public void FileSort() {
        getAllCatalog(new File(filePath));
        fileArrayList.sort((File file01, File file02) -> {
            String absolutePath = file01.getAbsolutePath();
            String absolutePath02 = file02.getAbsolutePath();
            Integer path01 = getPathName(absolutePath);
            Integer path02 = getPathName(absolutePath02);
            if (path01 > path02) {
                return 1;
            } else if (path01 < path02) {
                return -1;
            } else {
                return 0;
            }
        });
    }

    /**
     * D:\BaiduNetdiskDownload\Node.js入门到企业Web开发中的应用\63\53339195_63_0.flv
     *
     * @param absolutePath
     * @return
     */
    private Integer getPathName(String absolutePath) {
        String subStr = absolutePath.substring(0, absolutePath.lastIndexOf("_"));
        String count = subStr.substring(subStr.lastIndexOf("_") + 1);
        return Integer.parseInt(count);
    }

    public void getAllCatalog(File file) {
        File[] arrFiles = file.listFiles();
        for (int i = 0; i < arrFiles.length; i++) {
            if (arrFiles[i].isDirectory()) { //是文件就递归
                getAllCatalog(arrFiles[i]);
            } else {
                if (arrFiles[i].getAbsolutePath().contains(".flv")) {
                    fileArrayList.add(arrFiles[i]);
                }
            }
        }
    }


    @Test
    public void Main() throws IOException {
        init();
        FileSort();
        // fileNameArrayList.forEach(System.out::println);
        //fileArrayList.forEach(System.out::println);
        if (fileArrayList.size() == fileNameArrayList.size()) {
            for (int i = 0; i < fileNameArrayList.size(); i++) {
                File file = fileArrayList.get(i);
                String fileName = fileNameArrayList.get(i);
                FileUtils.copyFile(file, new File(copyPath + "/" + fileName + ".flv"));
            }
        }

    }


}

二、删除指定后缀的文件

public class FileDemo {
    private int count = 0;
    /**原始文件夹*/
    private static String filePath = "E:/慕课/redis从入门到高可用";

    private void getAllCatalog(File file) {
        File[] arrFiles = file.listFiles();
        for (int i = 0; i < arrFiles.length; i++) {
            if (arrFiles[i].isDirectory()) { //是文件就递归
                getAllCatalog(arrFiles[i]);
            } else {
                if (arrFiles[i].getAbsolutePath().endsWith("(1).mp4")) {
                    System.out.println(arrFiles[i].getAbsolutePath());
                    count++;
                    //删除重复的
                    arrFiles[i].delete();
                }
            }
        }
    }
    @Test
    public void Main() {
        getAllCatalog(new File(filePath));
        System.out.println(count);
    }
}

 

 类似资料: