请问如何吧这个程序的结果输出到一个txt文件中啊?

xiars123 2011-06-02 02:19:22
package URLUtil;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class URLUtil {

public static String getHtml(String urlString) {
try {
StringBuffer html = new StringBuffer();
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
InputStreamReader isr = new InputStreamReader(conn.getInputStream());
BufferedReader br = new BufferedReader(isr);
String temp;
while ((temp = br.readLine()) != null) {
html.append(temp).append("\n");
}
br.close();
isr.close();
return html.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}


public static void main(String[] args) {
FilterTags ft = new FilterTags();

System.out.println(ft.getFilterTags(URLUtil.getHtml("http://www.baidu.com")));

}
}
...全文
147 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
xiars123 2011-06-02
  • 打赏
  • 举报
回复
晕,原来是之前的忘记加bw.close()...
也谢谢楼上的帮忙,csdn都是活雷锋!
hy158753228 2011-06-02
  • 打赏
  • 举报
回复
随手写了个:

package csdn.impulsehu.may;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;

public class URLUtil {
public static void main(String[] args) {
pageContentToLocalFile("http://www.baidu.com", new File("baidu.txt"));
}

public static void pageContentToLocalFile(String sourceUrl, File destFileName) {
URL url = null;
InputStream is = null;
BufferedReader br = null;
FileWriter fw = null;
BufferedWriter bw = null;
try {
url = new URL(sourceUrl);
is = url.openConnection().getInputStream();
br = new BufferedReader(new InputStreamReader(is));
if(destFileName != null) {
fw = new FileWriter(destFileName);
} else {
fw = new FileWriter(new File("temp.tmp"));
}
bw = new BufferedWriter(fw);

String line;
while(null != (line = br.readLine())) {
System.out.println(line);
bw.write(line);
bw.newLine();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
closeIOStream(bw, fw, br, is);
}

}

private static void closeIOStream(BufferedWriter bw, FileWriter fw,
BufferedReader br, InputStream is) {
if(null != bw) {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(null != fw) {
try {
fw.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(null != br) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if(null != is) {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
}
}
}

}

xiars123 2011-06-02
  • 打赏
  • 举报
回复
我把楼上说的用到了main中,但是txt文件里还是没有结果,请问这是怎么回事?
lh_fengyuzhe 2011-06-02
  • 打赏
  • 举报
回复
BufferedWriter bw = new BufferedWriter(new FileWriter("a.txt"));
bw.write(ft.getFilterTags(URLUtil.getHtml("http://www.baidu.com")));

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧