您的位置:首页 > 编程语言 > Java开发

用java将html网页中的文本信息存入txt文件?

2013-05-01 21:07 477 查看
public static String getContent(String strUrl,String fileOutPath,String fileName) {

try {

URL url = new URL(strUrl);

BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream(),"gb2312"));

String s = "";

StringBuffer sb = new StringBuffer("");

while ((s = br.readLine()) != null) {

sb.append(s);

}

br.close();

String tem=sb.toString();

StringBuilder sbu = new StringBuilder("");

int i=1;

int begin;

int end;

do{

begin=tem.indexOf(">",i);

end=tem.indexOf("<",begin);

sbu.append(tem.substring(begin+1, end)+"\r\n");

i=end+1;

}while(i<tem.length()-6);

String outputFile= fileOutPath+fileName+".txt";

FileOutputStream fos=new FileOutputStream(outputFile);

fos.write(sbu.toString().getBytes("gb2312"));

fos.close();

return sbu.toString();

} catch (Exception e) {

return "error open url:" + strUrl;

}

}

在这段程序中若把while(i<tem.length()-6);改为while(i<tem.length());就无法生成txt文件,这是为什么?还有就是这段代码该如何改进?
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: