如何通过html文件的用户获取运行时输入来使用Jsoup进行解析?

时间:2015-04-26 17:15:34

标签: java html jsoup

与下面的代码段一样:

File input = new File("Example.html");
    Document doc = Jsoup.parse(input, "UTF-8", "Example.html");
    Elements links = doc.select("a[href]");
           System.out.print("\nLinks: ");

我想要的只是用户输入他选择的文件名而不是这个硬编码的" Example.html"。

1 个答案:

答案 0 :(得分:0)

这样做:

public class Test {

    public static void main(String[] args) {
        String filename = args[0];
        File input = new File(filename);
        Document doc = Jsoup.parse(input, "UTF-8");
        Elements links = doc.select("a[href]");
        System.out.print("\nLinks: ");
    }

}

然后,要运行它,请执行

java myClassFile.class FileNameIWant.html

这就是String[] args方法中main的用途。