VOOZH about

URL: https://www.geeksforgeeks.org/dsa/fast-io-in-java-in-competitive-programming/

⇱ Fast I/O in Java in Competitive Programming - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Fast I/O in Java in Competitive Programming

Last Updated : 6 Nov, 2025

In competitive programming, fast input and output (I/O) operations are essential to prevent Time Limit Exceeded (TLE) errors. Since Java’s standard I/O methods are relatively slow, optimizing I/O is crucial when dealing with large datasets.

  • Fast I/O helps handle large inputs efficiently in a limited time.
  • Using classes like BufferedReader, StringTokenizer, or custom FastReader boosts performance over Scanner.

Different Ways to Achieve Faster I/O in Java

1. Scanner Class

The Scanner Class in Java is slow for competitive Programming because it uses built-in methods like nextInt(), nextLong(), nextDouble(), etc., which come with additional overhead for parsing and input handling.

Note: The Scanner class is easy to use and requires less typing, but it's slower and not recommended for performance-critical tasks.

Output:

👁 Output
Output

2. BufferedReader

BufferedReader reads input in larger chunks, making it faster. However, you need to manually parse values using Integer.parseInt() and handle tokens using StringTokenizer.

Note: Buffered Reader is fast but not recommended as it requires a lot of typing.

Output:

👁 Screenshot-
Output

3. User-defined FastReader Class

The FastReader combines BufferedReader and StringTokenizer for maximum speed and ease of use. It is widely used in competitive programming.

Note: This approach is more flexible and faster for handling multiple types of data but may require additional coding.


Output:

👁 Output
Output

4. Using Reader Class 

This method is the fastest but not recommended because it's complex and more advanced. It uses InputStream and read() or nextInt(), which makes it difficult to remember and It is not beginner-friendly.

Output:

👁 Output
Output
Comment
Article Tags: