我必须调用一个以XML格式返回大量数据的休息Web服务。数据大小约为490米。每次尝试拨打服务时,都会耗尽内存。我想要做的就是将这些数据写入一个文件。
有没有办法以小块读取和写入数据以避免内存不足?
这是我的尝试;
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();
}
}
}