Java语言编写一个html整理的工具,实现按照title分类并且放入子目录,相同的放在一起

随笔,笔记

导入 依赖

<dependencies>
    <dependency>
        <groupId>org.jsoup</groupId>
        <artifactId>jsoup</artifactId>
        <version>1.13.1</version>
    </dependency>
</dependencies>

分类代码

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;
import java.util.LinkedList;
import java.util.List;

public class JavaParseH5 {

    // 若 出现 同名文件 进行区分
    private int id = 1;

    /**
     *  进行分类
     *  通过 Html 文件中的 Title 分类
     * @param oldMkdir 需要被分类的文件夹
     * @param newMkdir 需要 分类 到 的文件夹
     */
    public void classFiles(String oldMkdir,String newMkdir) throws IOException {

        // 获取 指定文件 夹下的所有 html 文件
        List<String> files = this.getFiles(oldMkdir, ".html");
        // 新的 文件 是否存在,不存在 进行创建
        if (this.createMkdir(newMkdir)){
            for (String file : files){
                System.out.println("---->"+file);
                // 获取文件的名字:\xxx.html
                String fileName = file.substring(file.lastIndexOf("\\")+1);
                // 根据 title 拼接 完整 新分类 的文件夹 路径
                String newUrl = newMkdir+"\\"+this.getHtmlTitle(file);
                System.out.println("+++++>"+newUrl);
                if (this.createMkdir(newUrl)){
                    // 将文件 分类 到 指定位置
                    String nurl = this.createFile(newUrl,fileName);
                    System.out.println(">>>>>>"+nurl);
                    this.copyFile(file,nurl);
                }
            }
        }
    }

    private String getHtmlTitle(String url) throws IOException {
        Document document = Jsoup.parse(new File(url), "utf-8");
        Elements title = document.getElementsByTag("title");
        Element first = title.first();
        // 若 文件 中没有 title 标签 会 默认生成一个 NotTitle 的文件夹
        return first == null?"NotTitle":first.text();
    }
    /**
     * 获取文件夹 下的 所有文件
     * @param path 文件夹 路径
     * @return
     */
    private List<String> getFiles(String path,String suf) {
        List<String> files = new LinkedList<String>();
        File file = new File(path);
        // 所有 文件的
        File[] tempList = file.listFiles();

        for (File value : tempList) {
            if (value.isFile()) {
                if (value.getName().substring(value.getName().length()-suf.length()).equals(suf)){
                    files.add(value.toString());
                }
            }
            if (value.isDirectory()) {
                List<String> files1 = this.getFiles(value.toString(), suf);
                files.addAll(files1);
            }
        }
        return files;
    }

    /**
     * 判断 当前 文件夹中是否存在指定文件 ,若存在 文件名字前会添加 一个随机串
     * @param url
     * @param name
     * @return
     */
    private String createFile(String url,String name) throws IOException {
        String l = url+"\\";
        File file = new File(l+name);
        if (file.exists()) {
            this.id++;
            name = this.id+name;
        }
        return l+name;
    }
    /**
     *  创建文件夹
     * @param mkdir 文件夹 的 路径
     * @return 文件夹 是否创建成功
     */
    private boolean createMkdir(String mkdir){
        File dir = new File(mkdir);
        if (!dir.exists()) {// 判断目录是否存在
            return dir.mkdir();
        }
        return true;
    }

    /**
     *  复制 文件 到指定的 其他目录的 文件中
     * @param source 原文件所在的位置
     * @param dest 复制到 的文件 位置
     * @throws IOException
     */
    private void copyFile(String source, String dest) throws IOException {
        FileChannel inputChannel = null;
        FileChannel outputChannel = null;
        try {
            inputChannel = new FileInputStream(new File(source)).getChannel();
            outputChannel = new FileOutputStream(new File(dest)).getChannel();
            outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
            System.out.println("?????");
        } finally {
            inputChannel.close();
            outputChannel.close();
        }
    }
}

测试

import java.io.IOException;

public class Main {

    public static void main(String[] args) throws IOException {

        JavaParseH5 h5 = new JavaParseH5();

        // 输入 你想 分类到 指定文件夹 比如 分类到:D:\htmlTest 文件夹下。
        // 输入 你想对 那个目录下 的 HTML 文件 进行分类 如:你想对 D:\test 文件夹的文件 进行分类
        h5.classFiles("D:\\htmlTest","D:\\htmlTest2");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
很好,以下是一个简单的示例,展示如何在 HTML 中使用 Java 连接数据库: ```html <!DOCTYPE html> <html> <head> <title>Java Database Connection</title> </head> <body> <h1>Java Database Connection Example</h1> <%-- Import the required Java classes --%> <%@ page import="java.sql.*" %> <%-- Define the database connection parameters --%> <% String url = "jdbc:mysql://localhost:3306/mydatabase"; %> <% String username = "myusername"; %> <% String password = "mypassword"; %> <%-- Attempt to establish a database connection --%> <% Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection(url, username, password); out.println("Database connection successful!"); } catch (SQLException e) { out.println("SQL Exception: " + e.getMessage()); } catch (ClassNotFoundException e) { out.println("Class Not Found Exception: " + e.getMessage()); } finally { try { if (conn != null) { conn.close(); } } catch (SQLException e) { out.println("SQL Exception: " + e.getMessage()); } } %> </body> </html> ``` 在这个示例中,我们使用 `<%@ page import %>` 指令导入了 `java.sql.*` 包中的所有类。然后,我们定义了连接到数据库所需的 URL、用户名和密码。 接下来,我们使用 `DriverManager.getConnection()` 方法尝试与数据库建立连接。如果连接成功,我们将在页面上显示一条成功消息,否则将显示一个 SQL 异常或类未找到异常。 最后,我们使用 `conn.close()` 方法关闭数据库连接。注意,在 `finally` 块中关闭连接可以确保在出现异常时也会关闭连接。 请注意,这只是一个简单的示例,应该根据您的具体需求进行修改和定制。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值