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...");

したがって、ライターは残りのデータ ストリームをファイルにコミットできます。

于 2013-11-14T20:01:20.283 に答える