Java fast reading and fast output

As we all know, the reading of the Java Scanner class is really slow.
When we use the Scanner class to read data, once the data exceeds 10,000, he will appear very slow
. The StreamTokenizer class is almost 300ms faster than Scanner.
Although this time feels very short! ! But in the algorithm implementation, 300ms is already a very large number, and he is very likely to make us feel what is TLE TTTTTT

So here I suggest that when we use Java to read in relatively large data, we can use the StreamTokenizer class.
The following describes his usage
(1) When we use him, we need to import the io package, and the class in the io package
(2) in When using this class, the function should throws IOException
(3) StreamTokenizer re = new StreamTokenizer (new BufferedReader (new InputStreamReader (System.in))); This is his specific implementation

There is a method in the StreamTokenizer class is nval. This method is the method we use to read data. It is of type double by default, so we must cast it to the type when reading it.
Let's look at the implementation.

import java.io.*;
public class test {
	public static void main(String args[]) throws IOException{
		StreamTokenizer re = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
		re.nextToken(); int n = (int)re.nval;
		System.out.println(n);
	}
}

You can see here that I used a nextToken () method. This method must be written every time we read data, that is, we must write this method before reading a data. For
example, if I still To read an m, then I have to write
re.nextToken (); int m = (int) re.nval; In short, it is right to read a write once

Someone will ask below, you can only read integers, how do I read strings?
When we read the string, we do not need to add the StreamTokenizer class.
We can directly write BufferedReader re = new BufferedReader (new InputStreamReader (System.in));
see the code

import java.io.*;
public class test {
	public static void main(String args[]) throws IOException{
//		StreamTokenizer re = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
		BufferedReader re = new BufferedReader(new InputStreamReader(System.in));
		String x = re.readLine();
		System.out.println(x);
	}
}

In this way, we can read the string. Of course, if we use this method to read the string, we do n’t need the previous nextToken method.
Someone will say it below, then I want to read the number together with the string What should I do?
Let's look at the code

import java.io.*;
public class test {
	public static void main(String args[]) throws IOException{
		BufferedReader re = new BufferedReader(new InputStreamReader(System.in));
		StreamTokenizer r = new StreamTokenizer(re);
		r.nextToken(); int n = (int)r.nval;
		String x = re.readLine();
		System.out.println(n+" "+x);
//		输入 
//		3
//		afadfa
//		输出
//		3
	}
}

Please note that there will be a problem with the above writing. What we want to read is an integer and then a string, but what if we only read this integer here?
This is because when we read the integer, the integer is read away, but the carriage return after the integer is not read away. If we use readLine () to read directly, we will not be able to read the string on the next line, so here we You should add a readLine () to read out the carriage return,
see the code

import java.io.*;
public class test {
	public static void main(String args[]) throws IOException{
		BufferedReader re = new BufferedReader(new InputStreamReader(System.in));
		StreamTokenizer r = new StreamTokenizer(re);
		r.nextToken(); int n = (int)r.nval;
		re.readLine();
		String x = re.readLine();
		System.out.println(n+" "+x);
//		输入 
//		3
//		afadfa
//		输出
//		3 afadfa
	}
}

In this way, we can read integers and read strings. When the data is large, we recommend that you use this method to read in data.
Then we have to introduce a fast output. This input is also faster than the System output, so we will When writing programs with large data, you can write like this

import java.io.*;
public class test {
	public static void main(String args[]) throws IOException{
		BufferedReader re = new BufferedReader(new InputStreamReader(System.in));
		StreamTokenizer r = new StreamTokenizer(re);
		PrintWriter pr = new PrintWriter(new OutputStreamWriter(System.out));
		r.nextToken(); int n = (int)r.nval;
		re.readLine();
		String x = re.readLine();
		pr.println(n+" "+x);
		pr.flush();
//		输入 
//		3
//		afadfa
//		输出
//		3 afadfa
	}
}

When using PrintWriter output, remember to add a final flush (), ok, we will learn to read and output quickly. If you
have any questions, please leave a message

Published 32 original articles · praised 5 · visits 862

Guess you like

Origin blog.csdn.net/shizhuba/article/details/105068631