If there is no system Out, what about printing on the console?

I recently encountered a problem in an interview The problem statement is – > assuming that you cannot access the system class in the JDK API or use echo, how do you print anything on the console in the JRE 5 environment? The question really starts with - why did Java give us the printstream object system out ?? Why is it final? There is no other way to print anything on the console

Solution

If necessary, you can bypass the system object System. Out does a lot of extra things (such as dealing with Unicode), so if you really just want raw output and performance, you may actually even bypass it

import java.io.*;

public class PrintOutTest {
  public static void main(String args[]) throws IOException {
    BufferedWriter out = new BufferedWriter(new OutputStreamWriter(new
      FileOutputStream(FileDescriptor.out),"ASCII"),512);
    out.write("test string");
    out.write('\n');
    out.flush();
  }
}

This has been further elaborated here

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>