Java 高效读取多行 URLConnection
Java read URLConnection with many lines efficiently
我必须在 java.
中读取包含 2MB 漂亮印刷 JSON 的 URLConnection 响应
2mb 不是 "small" 但绝不大。它包含 JSON。但是,它打印得很好 JSON,大约有 60k 行。 A
while ((line = bufferedReader.readLine()) != null) {
lineAllOfIt += line;
}
阅读此回复大约需要 10 分钟。我的方法肯定有问题,但我想不出更好的方法。
对于这种特殊情况,我会使用 java 在本地缓存文件。您可以将文件以低内存传输到您的计算机,然后您可以逐行浏览它而无需将文件加载到内存并提取您需要的数据或一次加载所有数据。
编辑:对变量名进行了更改我从我的代码中提取了这个并忘记中和变量。 FileChannel transferTo/transferFrom 也可以更高效,因为可能有更少的副本,并且根据操作可以从 SocketBuffer -> Disk 进行。 FileChannel API
String urlString = "http://update.domain.com/file.json" // File URL Path
Path diskSaveLocation = Paths.get("file.json"); // This will be just help place it in your working directory
final URL url = new URL(fileUrlString);
final URLConnection conn = url.openConnection();
final long fileLength = conn.getContentLength();
System.out.println(String.format("Downloading file... %s, Size: %d bytes.", fileUrlString, fileLength));
try(
FileOutputStream stream = new FileOutputStream(diskSaveLocation.toFile(), false);
FileChannel fileChannel = stream.getChannel();
ReadableByteChannel inChannel = Channels.newChannel(conn.getInputStream());
) {
long read = 0;
long readerPosition = 0;
while ((read = fileChannel.transferFrom(inChannel, readerPosition, fileLength)) >= 0 && readerPosition < fileLength) {
readerPosition += read;
}
if (fileLength != Files.size(diskSaveLocation)) {
Files.delete(diskSaveLocation);
System.out.println(String.format("File... %s did not download correctly, deleting file artifact!", fileUrlString));
}
}
System.out.println(String.format("File Download... %s completed!", fileUrlString));
((HttpURLConnection) conn).disconnect();
您现在可以使用 NIO2 方法读取同一个文件,该方法允许您逐行读取而无需加载到内存中。使用 Scanner 或 RandomAccessFile 方法可以防止将行读入堆中。如果您想读取整个文件,您也可以使用 Java Files
实用方法中的许多方法从缓存文件本地读取。
Java Read Large Text File With 70million line of text
我必须在 java.
中读取包含 2MB 漂亮印刷 JSON 的 URLConnection 响应2mb 不是 "small" 但绝不大。它包含 JSON。但是,它打印得很好 JSON,大约有 60k 行。 A
while ((line = bufferedReader.readLine()) != null) {
lineAllOfIt += line;
}
阅读此回复大约需要 10 分钟。我的方法肯定有问题,但我想不出更好的方法。
对于这种特殊情况,我会使用 java 在本地缓存文件。您可以将文件以低内存传输到您的计算机,然后您可以逐行浏览它而无需将文件加载到内存并提取您需要的数据或一次加载所有数据。
编辑:对变量名进行了更改我从我的代码中提取了这个并忘记中和变量。 FileChannel transferTo/transferFrom 也可以更高效,因为可能有更少的副本,并且根据操作可以从 SocketBuffer -> Disk 进行。 FileChannel API
String urlString = "http://update.domain.com/file.json" // File URL Path
Path diskSaveLocation = Paths.get("file.json"); // This will be just help place it in your working directory
final URL url = new URL(fileUrlString);
final URLConnection conn = url.openConnection();
final long fileLength = conn.getContentLength();
System.out.println(String.format("Downloading file... %s, Size: %d bytes.", fileUrlString, fileLength));
try(
FileOutputStream stream = new FileOutputStream(diskSaveLocation.toFile(), false);
FileChannel fileChannel = stream.getChannel();
ReadableByteChannel inChannel = Channels.newChannel(conn.getInputStream());
) {
long read = 0;
long readerPosition = 0;
while ((read = fileChannel.transferFrom(inChannel, readerPosition, fileLength)) >= 0 && readerPosition < fileLength) {
readerPosition += read;
}
if (fileLength != Files.size(diskSaveLocation)) {
Files.delete(diskSaveLocation);
System.out.println(String.format("File... %s did not download correctly, deleting file artifact!", fileUrlString));
}
}
System.out.println(String.format("File Download... %s completed!", fileUrlString));
((HttpURLConnection) conn).disconnect();
您现在可以使用 NIO2 方法读取同一个文件,该方法允许您逐行读取而无需加载到内存中。使用 Scanner 或 RandomAccessFile 方法可以防止将行读入堆中。如果您想读取整个文件,您也可以使用 Java Files
实用方法中的许多方法从缓存文件本地读取。
Java Read Large Text File With 70million line of text