Java快速IO(ACM)必备 2019-06-12

 en....

无非用到的是

1. new Scanner(System.in);

2.new BUfferReader(new InputStreamReader(System.in);

3.System.in.read() //需要转换

当然1,2这两个还有许多坑要注意,比如说 next() 不会读入null字符,就一直等待,nextline() 吸收到缓冲区 但是不会读入,readLine 与read()也是一样

这里需要注意:in.nextInt() 接着 in.nextLine()会出现读入空缺现象,很明显,我前面说过,nextLine会吸收到缓冲区

贴代码:

 

package IO;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.util.StringTokenizer;/** * Class for buffered reading int and double values */class Reader解决超时 { static BufferedReader reader; static StringTokenizer tokenizer; /** * call this method to initialize reader for InputStream */ static void init(InputStream input) { reader = new BufferedReader( new InputStreamReader(input)); tokenizer = new StringTokenizer(""); } /** * get next word */ static String next() throws IOException { while (!tokenizer.hasMoreTokens()) { //TODO add check for eof if necessary tokenizer = new StringTokenizer( reader.readLine()); } return tokenizer.nextToken(); } static String nextLine()throws IOException{ while(!tokenizer.hasMoreElements()){ tokenizer=new StringTokenizer( reader.readLine() ); } return tokenizer.nextToken(""); } static int nextInt() throws IOException { return Integer.parseInt(next()); } static double nextDouble() throws IOException { return Double.parseDouble(next()); } public static void main(String[] args) throws IOException{ init(System.in); StringTokenizer st = new StringTokenizer("this is a test"); while (st.hasMoreTokens()) { System.out.println(st.nextToken()); } // System.out.println(next()); System.out.println(nextLine()); }}

 

Copyright © 2019 优发国际登陆 All Rights Reserved
许必祥
地址:黑龙江省哈尔滨市道里区安静街29号
全国统一热线:18557279476