0

...

public static void download() {
             try {
                 URL oracle = new URL("http://api.wunderground.com/api/54f05b23fd8fd4b0/geolookup/conditions/forecast/q/US/CO/Denver.json");
                 BufferedReader in = new BufferedReader(new InputStreamReader(oracle.openStream()));

                 File file = new File("C:\\Users\\User\\Desktop\\test2.json");
                 if (!file.exists()) {
                     file.createNewFile();
                 }
                 FileWriter fw = new FileWriter(file.getAbsoluteFile());
             BufferedWriter bw = new BufferedWriter(fw);

                 String inputLine;
                 while ((inputLine = in.readLine()) != null) {
                     bw.write(inputLine+"\n");
                 }
                 fw.close();
                 in.close();
                 System.out.println("Finished...");
             }
             catch(MalformedURLException e){e.printStackTrace();}
             catch(IOException e){e.printStackTrace();}
         }

我正在制作一个网络爬虫来从 Wunderground 检索天气更新。我确实让它工作了,但它没有解析整个文档(最后一点的剪辑)。我做错了什么?

4

1 回答 1

1

你用一个包裹你FileWriterBufferedWriter

FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);

但只关闭FileWriter

fw.close();

由于FileWriter没有访问权限并且不知道BufferedWriter,它不会刷新任何可能的剩余缓冲区。您可以调用flush()onbw或 closebw而不是fw. 调用bw.close()将负责关闭包装的FileWriter.

bw.close();
in.close();
于 2013-11-14T19:13:59.627 回答