请高手帮忙解决一下:

1.我想提取出1.html 的<title>之间的内容给它重新命名,可是下面的代码一直不能成功,请帮我看看问题出在哪儿?谢谢了

2.另外提取网页某个标签之间的内容用什么方法最好?

我学java一个月 呵呵~package t0624;
import java.io.*;public class TestMyChangeName
{ public static void main(String[] args)
{
File f = new File("c:\\test\\1.html");
changeName(f);
} public static void changeName(File f)
{
// 提取html的title之间的内容给一个html取名
try
{
FileReader fr = new FileReader(f);
BufferedReader br = new BufferedReader(fr);
String str, name;
while ((str = br.readLine()) != null)
{
if (str.contains("<title>"))
{

name = str.replaceAll("</?[a-zA-Z]+>", "");//替换掉<title>和</title>
System.out.println(name);
f.renameTo(new File("c:\\test\\"+name+".html"));
return;//
}
}
} catch (Exception exc) {}
}
}

解决方案 »

  1.   

    caoxiongjun 我不大明白你的意思~
    你说的是把什么写回文件呢?我目前的做法,用bufferreader读取title的内容然后改名,哪一步出错了,请说具体一点好吗?
      

  2.   

    我装的是jdk1.4所以用的indexOf方法
    主要是添加了两行close() public static void changeName(File f)
    {
    try
    {
    FileReader fr = new FileReader(f);
    BufferedReader br = new BufferedReader(fr);
    String str, name;
    while ((str = br.readLine()) != null)
    {
    if (str.toUpperCase().indexOf("<TITLE>")>=0 )
    {

    name = str.replaceAll("</?[a-zA-Z]+>", "");//替换掉<title>和</title>
    System.out.println(name);
    br.close();//这里
    fr.close();//这里

    f.renameTo(new File("c:\\test\\"+name+".html"));
    return;
    }
    }
    } catch (Exception exc) {
    System.out.println(exc);
    }

    }