Java工具 java实现从网页上复制表格,自动生成Markdown形式表格

java实现从网页上复制表格

需求分析

例如我这里有一个表格:

表格里的文本是可复制的,复制下来的文本如下。

序号  方法  描述
1   public URL(String spec) throws MalformedURLException    根据 String 表示形式的地址创建 URL 对象。
2   public URL(String protocol,String host,int port,String file) throws MalformedURLException   创建URL对象,并指定协议protocol,主机host,端口名称port,资源文件名file
3   public URLConnection openConnection() throws IOException    返回一个 URLConnection 对象,它表示到 URL 所引用的远程对象的连接。
4   public final InputStream openStream() throws IOException    打开到此 URL 的连接并返回一个用于从该连接读入的 InputStream

现在需要,把这个复制下来的文本生成markdown表格。

基本实现

观察这个文本,我发现,每个单元格之间不多不少刚好有两个空白符,所以,只要发现有两个空白符的地方我就认为表格中单元格之间的边界。
代码如下所示:

package regex.tools;

import java.util.Scanner;
import clipboard.util.SysClipboardUtil;

public class MDtable
{
    public static void main(String[] args)
    {
        String text=SysClipboardUtil.getSysClipboardText();
        System.out.println(text);
        Scanner scanner=new Scanner(text);
        String line;
        String[] fragments;
        System.out.println("-------------------------------------------");
        boolean flag=true;
        while(scanner.hasNextLine())
        {
            line=scanner.nextLine();
            //刚好两个的地方分割
            fragments=line.split("\\s{2}");
//          System.out.println("line:--->");
//          System.out.println("fragments:");
            System.out.print("|");
            for (String string : fragments)
            {
                System.out.print(string+"|");
            }
            System.out.println();
            //打印表格对其方式,使用默认对齐方式
            if(flag)
            {
                System.out.print("|");
                for(int i=0;i<fragments.length;i++)
                {
                    System.out.print("-|");
                }
                System.out.println();
                flag=false;
            }
        }
    }
}

测试

复制表格中的文字

然后运行上面的代码,运行结果如下。

序号  方法  描述
1   public URL(String spec) throws MalformedURLException    根据 String 表示形式的地址创建 URL 对象。
2   public URL(String protocol,String host,int port,String file) throws MalformedURLException   创建URL对象,并指定协议protocol,主机host,端口名称port,资源文件名file
3   public URLConnection openConnection() throws IOException    返回一个 URLConnection 对象,它表示到 URL 所引用的远程对象的连接。
4   public final InputStream openStream() throws IOException    打开到此 URL 的连接并返回一个用于从该连接读入的 InputStream
-------------------------------------------
|序号|方法|描述|
|-|-|-|
|1|public URL(String spec) throws MalformedURLException|根据 String 表示形式的地址创建 URL 对象。|
|2|public URL(String protocol,String host,int port,String file) throws MalformedURLException|创建URL对象,并指定协议protocol,主机host,端口名称port,资源文件名file|
|3|public URLConnection openConnection() throws IOException|返回一个 URLConnection 对象,它表示到 URL 所引用的远程对象的连接。|
|4|public final InputStream openStream() throws IOException|打开到此 URL 的连接并返回一个用于从该连接读入的 InputStream|

效果

把markdown表格代码粘贴到markdown文档中,效果如下。

序号方法描述
1public URL(String spec) throws MalformedURLException根据 String 表示形式的地址创建 URL 对象。
2public URL(String protocol,String host,int port,String file) throws MalformedURLException创建URL对象,并指定协议protocol,主机host,端口名称port,资源文件名file
3public URLConnection openConnection() throws IOException返回一个 URLConnection 对象,它表示到 URL 所引用的远程对象的连接。
4public final InputStream openStream() throws IOException打开到此 URL 的连接并返回一个用于从该连接读入的 InputStream

可以看到这样就很容易的复制一个表格了。

代码优化

Java复制网上表格的方法

把上面的代码封装成方法:

/**
 * 生成markdown表格。
 * @param text 复制的表格文字
 * @return markdown表格代码
 */
public static String toMarkdownTable(String text)
{
    StringBuilder sBuider=new StringBuilder();
    Scanner scanner=new Scanner(text);
    String line;
    String[] fragments;
    boolean flag=true;
    while(scanner.hasNextLine())
    {
        line=scanner.nextLine();
        //刚好两个的地方分割
        fragments=line.split("\\s{2}");
//          System.out.print("|");
        sBuider.append("|");
        for (String string : fragments)
        {
//              System.out.print(string+"|");
            sBuider.append(string+"|");
        }
//          System.out.println();
        sBuider.append("\n");
        //打印表格对其方式,使用默认对齐方式
        if(flag)
        {
//              System.out.print("|");
            sBuider.append("|");
            for(int i=0;i<fragments.length;i++)
            {
//                  System.out.print("-|");
                sBuider.append("-|");
            }
//              System.out.println();
            sBuider.append("\n");
            flag=false;
        }
    }
    return sBuider.toString();
}

调用方式

调用:

public static void main(String[] args)
{
    //从剪贴板获取文本
    String text=SysClipboardUtil.getSysClipboardText();
    //写回剪贴板
    SysClipboardUtil.setSysClipboardText(toMarkdownTable(text));
}

运行结果

控制台没有输出,markdown表格代码已经写到剪贴板中去了,不再粘贴,效果和上面的一致。

依赖类:

依赖类:clipboard.util.SysClipboardUtil

完整的代码:

package regex.tools;

import java.util.Scanner;
import clipboard.util.SysClipboardUtil;

public class MDtable
{
    public static void main(String[] args)
    {
        // 从剪贴板获取文本
        String text = SysClipboardUtil.getSysClipboardText();
        // 写回剪贴板
        SysClipboardUtil.setSysClipboardText(toMarkdownTable(text));
    }

    /**
     * 生成markdown表格。
     * 
     * @param text
     *            复制的表格文字
     * @return markdown表格代码
     */
    public static String toMarkdownTable(String text)
    {
        StringBuilder sBuider = new StringBuilder();
        Scanner scanner = new Scanner(text);
        String line;
        String[] fragments;
        // System.out.println("-------------------------------------------");
        boolean flag = true;
        while (scanner.hasNextLine())
        {
            line = scanner.nextLine();
            // 刚好两个的地方分割
            fragments = line.split("\\s{2}");
            // System.out.println("line:--->");
            // System.out.println("fragments:");
            // System.out.print("|");
            sBuider.append("|");
            for (String string : fragments)
            {
                // System.out.print(string+"|");
                sBuider.append(string + "|");
            }
            // System.out.println();
            sBuider.append("\n");
            // 打印表格对其方式,使用默认对齐方式
            if (flag)
            {
                // System.out.print("|");
                sBuider.append("|");
                for (int i = 0; i < fragments.length; i++)
                {
                    // System.out.print("-|");
                    sBuider.append("-|");
                }
                // System.out.println();
                sBuider.append("\n");
                flag = false;
            }
        }
        return sBuider.toString();
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值