Java 输入输出效率优化黑科技get

刚开始,用最常规的输入输出:

import java.util.*;
Scanner scan=new Scanner(System.in);
System.out.println("...");

面对海量输入输出数据,TLE妥妥的

接着有了下面这种姿势(输入流?):

import java.util.*;
import java.io.*;
Scanner scan=new Scanner(new BufferedInputStream(System.in));

效果不太理想。
最后查阅大神用于处理水题的代码,获得以下终极模板:

import java.io.*;
/*...*/
public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
while(in.nextToken() != StreamTokenizer.TT_EOF){
    n=(long)in.nval;
    in.nextToken();
    m=(long)in.nval;
    out.println(n+" "+m);
}
out.flush();//刷新。若本行放循环体内,则毎输入一组数据即输出一组答案,但是效率降低
out.close();
}

Java 输入输出效率优化黑科技get

https://devblog.citruxonve.net/posts/47eab411/

Author

Semprathlon / Simfae Dean

Posted on

05/31/2015

Updated on

07/19/2023

Licensed under

Comments