我必须调用一个以 xml 形式返回大量数据的 rest web 服务。数据大小约为490m。每次我尝试调用该服务时,我都会内存不足。我要做的就是将此数据写入文件。
有没有办法以小块读取和写入数据以避免内存不足?
这是我尝试过的;
public class GetWs {
   private static String url ="http://somewebservice";
   public static void main(String[] args) {
    InputStream in;
    OutputStream out;
    try {
          out = new FileOutputStream("testoutfile.txt");
          in = new URL(url).openStream();
          int b;
          do {
               b = in.read();
               if (b != -1) {
            out.write(b);
                 out.flush();
               }
           } while (b != -1);
            in.close();out.close();     
    } catch (Exception e) {
        e.printStackTrace();
     }
   }
}