用java将html网页中的文本信息存入txt文件?
2013-05-01 21:07
543 查看
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文件,这是为什么?还有就是这段代码该如何改进?
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文件,这是为什么?还有就是这段代码该如何改进?
相关文章推荐
- java捕获异常信息存入txt文件示例
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- VFP中,如何把指定目录信息(包括子目录)存入TXT文本文件!(已解决,代码实例)
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例 .
- 网页大文本文件的在线加载预览,txt,xml,html等纯文本格式
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- JAVA读取WORD,EXCEL,PDF,TXT,RTF,HTML文件文本内容的方法示例
- Java实现追加换行写信息到.txt文件
- Java操作读取写入文本TXT及XML文件内容
- java获取网页上邮箱地址存放到.txt文件
- java创建txt文件并存入内容
- Java读写文件,读取execl表格,读取指定行列单元格信息,拼接sql,写入TXT文档。