Open In App

How to Print Fast Output in Competitive Programming using Java?

Last Updated : 16 Aug, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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:

Java




// Print fast Output in Competitive Programming using JAVA
import java.io.*;
 
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");
        }
 
        output.flush();
    }
}


Output

Geeks
For
Geeks

 


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads