0
import java.io.*;
import java.util.*;
import java.net.*;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

public class URLReader {

     public static void main(String[] args) {
         download();
     }

     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");
             }
             in.close();
             System.out.println("Finished with no errors...");
         }
         catch(MalformedURLException e){System.out.println("err1");}
         catch(IOException e){System.out.println("err2");}
     }
}

我正在尝试将此 JSON 文件下载到我的计算机上,但是它很快就停止了。它在第 8192 个字符之后结束,不再消失。否则它工作正常,关于我做错了什么的任何想法?

这也是下载网页源的正确方法,有人可以给我一些更好的技术来帮助我吗?

4

1 回答 1

1

您忘记关闭 BufferedWriter :-)

in.close();
bw.close(); // Add this here
System.out.println("Finished with no errors...");

所以 Writer 可以将剩余的数据流提交到文件中

于 2013-11-14T20:01:20.283 回答