【发布时间】:2019-01-30 05:32:00
【问题描述】:
我想从大 txt 文件中删除重复数字,其中第一行是行中排序数字的数量,但有内存限制 - 20 mb。我阅读了How to Read a Large File 的文章并尝试使用BufferedWriter,但该过程需要超过20 mb。 请帮帮我。
import java.io.*;
import java.util.Scanner;
public class DeleteRepeatingNumbers {
public static void main(String[] args) throws IOException {
try (Scanner sc = new Scanner(
new FileInputStream("input.txt"), "UTF-8");
Writer writer = new BufferedWriter(
new OutputStreamWriter(
new FileOutputStream("output.txt"), "utf-8"))) {
int n = sc.nextInt();
int prevInt = 0;
if (n != 0) {
prevInt = sc.nextInt();
writer.write(String.valueOf(prevInt));
}
for (int i = 0; i < n - 1; i++) {
int next = sc.nextInt();
if (next != prevInt) {
writer.write(System.getProperty("line.separator"));
writer.write(String.valueOf(next));
prevInt = next;
}
}
}
}
}
例子:
21
2
4
8
8
8
9
11
11
11
11
11
11
13
14
15
16
222
222
222
222
222
【问题讨论】:
-
整个 jvm 需要 20mb 吗?这将是困难的。 jvm怎么启动,出现哪个错误?
-
它只用于缓冲区
-
你在哪里看到超过 20mb 被使用了?我不明白你的问题
-
你在哪里看到超过 20mb 被使用了?我不明白你的问题
标签: java memory io nio large-files