`

IE收藏夹转化为单个链接的代码(仅用IE8测试了)

阅读更多

鉴于公司只能通过博客发表文章,并且每篇文章的大小是有限制的,但是不至于我们辛苦的付出就变成了将来即使离职也带不走的东西,那就要想法子把自己所学东西带出来,哈哈。

 

用IE的导出功能把收藏夹导出为一个htm文件,然后通过java代码从中抽取链接并输出,代码如下:

 

 import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class HtmlReader { 
 
 public static void main(String[] args) throws IOException {
  File file = new File("d:/bookmark.htm");
  FileReader fr = new FileReader(file);
  BufferedReader br = new BufferedReader(fr);
  String str = null;
  String temp = null;
  int startindex = -1;
  int endindex=-1;
  String globalstr = null;
  //是否需要换一行链接字符串
  boolean contactNextLine = false;
  //当前行号
  int linenum = 0;
  //下一行行号
  int nextLineNum = 0;
  //页面数量
  int count = 0;
  while((str=br.readLine())!=null)
  {
   linenum ++;
   //输出当前网页所属类别,IE8的源代码类似:<H3 FOLDED ADD_DATE="1347527409">CXF</H3>
   if(str.contains("FOLDED ADD_DATE"))
   {
    startindex=str.lastIndexOf("\"");
    temp = str.substring(startindex+2);
    startindex=temp.indexOf("</");
    temp = temp.substring(0,startindex);
    System.out.println("-----"+temp+"-----");
   }
   

if(str.contains("HREF"))
{
startindex = str.indexOf("\"");
//获取 href" 后面的内容
temp = str.substring(startindex+1);
//找第二个引号,格式为:href="xxx.html"
endindex = temp.indexOf("\"");
//说明链接太长没有显示完毕,需要拼接下行的字符串
if(endindex==-1)
{
globalstr = temp;
contactNextLine = true;
nextLineNum = linenum+1;
}
else {
str = temp.substring(0,endindex);
count ++;
System.out.println(str);
}
}
else {
//注意:这里假设最多只有两行来拼接一个链接
if(nextLineNum==linenum)
{
if(contactNextLine)
{
count ++;
startindex = str.indexOf("\"");
temp = str.substring(0,startindex);
str = globalstr + temp;
globalstr = null;
contactNextLine = false;
System.out.println(str);
}
}
}

}
System.out.println("共有页面数:"+count);
}

}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics