我正在尝试使用以下代码从标准输入读取一行大约200万个字符的单行:
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
s = in.readLine();
对于上述输入,s = in.readLine();
行需要30多分钟才能执行。
是否有更快的方式来阅读此输入?
答案 0 :(得分:1)
不要尝试逐行阅读,而是尝试读取字符缓冲区
尝试这样的事情
BufferedInputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream("your-file"));
byte[] data = new byte[1024];
int count = 0;
while ((count = in.read(data)) != -1) {
// do what you want - save to other file, use a StringBuilder, it's your choice
}
} catch (IOException ex1) {
// Handle if something goes wrong
} finally {
if (in != null) {
try {
in.close();
} catch (IOException ) {
// Handle if something goes wrong
}
}
}