效率高于split()
public StringTokenizer(String str,String delim,boolean returnTokens);
public StringTokenizer(String str,String delim);
public StringTokenizer(String str);
int countTokens():返回nextToken方法被调用的次数
boolean hasMoreTokens():返回是否还有分隔符
boolean hasMoreElements():返回是否还有分隔符
String nextToken():返回从当前位置到下一个分隔符的字符串
Object nextElement():返回从当前位置到下一个分隔符的字符串
String nextToken(String delim):与4类似,以指定的分隔符返回结果
import java.io.*;
import java.util.*;
class InputReader {
private final static int BUF_SZ = 65536;
private BufferedReader in;
private StringTokenizer tokenizer;
InputReader(InputStream buf) {
this.in = new BufferedReader(new InputStreamReader(buf), BUF_SZ);
tokenizer = new StringTokenizer("");
}
private String next() {
while (!tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(in.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken().trim();
}
int nextInt() {
return Integer.parseInt(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
public class Main {
public static void main(String[] args) {
// write your code here
var cin = new InputReader(System.in);
System.out.println(cin.nextInt());
System.out.println(cin.nextDouble());
}
}
Sample Input
10010 10086.1
Sample Output
10010
10086.1
Java 不使用Scanner读取System.in输入流(自己实现一个InputReader)
原文:https://www.cnblogs.com/YY666/p/11787812.html