【问题标题】:HTML Parser for multiple input files用于多个输入文件的 HTML 解析器
【发布时间】:2012-06-22 18:53:45
【问题描述】:

我想一次选择多个 html 文件并使用 html 解析器仅提取文本,每个 html 文件将创建一个单独的文本文件。任何人都可以为此建议java代码。

`FileReader f0 = new FileReader("j.html");
StringBuilder sb = new StringBuilder();
 BufferedReader br = new BufferedReader(f0);
while((temp1=br.readLine())!=null)
  { sb.append(temp1); }
String para = sb.toString().replaceAll("<br>","\n");
String textonly = Jsoup.parse(para).text();
System.out.println(textonly);
FileWriter f1=new FileWriter("j.txt");
char buf1[] = new char[textonly.length()];
textonly.getChars(0,textonly.length(),buf1,0);
for(i=0;i<buf1.length;i++) {
 if(buf1[i]=='\n')
f1.write("\r\n");
f1.write(buf1[i]);
}`

我有这段代码,但一次只占用一个文件。我想选择多个文件。

【问题讨论】:

标签: java javascript html oracle parsing


【解决方案1】:

你不能把你的代码放在一个循环中吗?类似的东西(未测试):

// loop over files you want to change
for (int i = 1; i < 1000; i++) {
   FileReader f0 = new FileReader(i + ".html");
   StringBuilder sb = new StringBuilder();
   BufferedReader br = new BufferedReader(f0);
   while((temp1=br.readLine())!=null) { 
      sb.append(temp1); 
   }
   String para = sb.toString().replaceAll("<br>","\n");
   String textonly = Jsoup.parse(para).text();
   System.out.println(textonly);
   // stick .txt on the end of the filename to write out
   FileWriter f1=new FileWriter(i + ".txt"); 
   char buf1[] = new char[textonly.length()];
   textonly.getChars(0,textonly.length(),buf1,0);
   for(i=0;i<buf1.length;i++) {
      if(buf1[i]=='\n') {
         f1.write("\r\n");
      }
      f1.write(buf1[i]);
   }

【讨论】:

  • @Disko 3 感谢您提出宝贵的建议。但是我有 2000 个文件,并且我已经给它们从 1 到 2000 的编号名称为“1.html”。所以我想给像 (i=1;i
  • 代码已更改为执行此操作。它只是使用简单的字符串处理来构造文件名,因此您可能需要阅读它。
  • 是的。我有解决方案...thanx @Disko 3
猜你喜欢
  • 2020-06-22
  • 1970-01-01
  • 1970-01-01
  • 2016-06-20
  • 2014-03-14
  • 1970-01-01
  • 2018-07-31
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多