为了账号安全,请及时绑定邮箱和手机立即绑定

为什么txt文档中只能输出读取到的html的最后一行呢?

为什么txt文档中只能输出读取到的html的最后一行呢?

慕粉4016800 2017-03-27 17:38:34
public static void main(String[] args) throws IOException { URL url = new URL("这里是网址"); URLConnection conn = url.openConnection();// 获得UrlConnection 连接对象 InputStream is = conn.getInputStream();// 获得输入流 BufferedReader br = new BufferedReader(new InputStreamReader(is));// buffered表示缓冲类 String str; FileWriter fw = null; while ((str = br.readLine()) != null) { fw = new FileWriter("D:\\1.txt"); System.out.println(str); fw.write(str); fw.flush(); } fw.close(); }求大神们赐教
查看完整描述

1 回答

已采纳
?
Caballarii

TA贡献1123条经验 获得超629个赞

你每次都开个新的FileWriter,当然是把前面的内容全覆盖了

把fw = new FileWriter("D:\\1.txt");移到循环外面先

查看完整回答
反对 回复 2017-03-27
  • 1 回答
  • 0 关注
  • 1406 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信