Skip to content
Related Articles

Related Articles

How to Print Fast Output in Competitive Programming using Java?

View Discussion
Improve Article
Save Article
  • Difficulty Level : Medium
  • Last Updated : 16 Aug, 2021

In Competitive programming, most of the students use C++ as their primary language as it is faster than the other languages(e.g Java, Python) but for a student/professional who use Java as his/her primary language taking Input from input streams and printing fast output is the main difficulty faced during contests on competitive platforms(eg. CodeChef, CodeForces, Spoj, etc).

In this article, there defined the fastest method to print O/P using Java (Mainly in Competitive Programming).

BufferedWriter Class: It writes text to a character-output stream, buffering characters to provide for the efficient writing of single characters, arrays, and strings. It makes the performance fast.

BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));

Methods of BufferedWriter:

  • write(): writes a single character to the internal buffer of the writer.
  • write(char[] array): writes the characters from the specified array to the writer.
  • write(String data): writes the specified string to the writer.
  • flush(): used to clear the internal buffer.
  • close(): used to close the buffered writer.

Below is the implementation of the problem statement:


// Print fast Output in Competitive Programming using JAVA
class GFG {
    public static void main(String[] args) throws Exception
        String[] gfg = { "Geeks", "For", "Geeks" };
        BufferedWriter output = new BufferedWriter(
            new OutputStreamWriter(System.out));
        for (int i = 0; i < gfg.length; i++) {
            output.write(gfg[i] + "\n");




My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!