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文档中,效果如下。
序号 | 方法 | 描述 |
---|---|---|
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 |
可以看到这样就很容易的复制一个表格了。
代码优化
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();
}
}