What to do without System.out, to print on console?
You could bypass the System object if you want to. System.out does a lot of extra stuff (handling unicode, for instance), so if you really want just the raw output and performance, you actually probably even should 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 elaborated a bit further in here.
PrintStream is final because it does everything the windows console /can/ do. Also its the same with "Console" being a const class in C#. The classes encapsulate everything the console can do, and it does in one specific way only. You can't "make it better" because at one point, it is upto the OS to handle it.
There are plenty of ways to output something on screen:
- Write your own OutputStream the way @eis did it
- Use JNI and invoke a method in a native DLL/SO that invokes a native function like
printf()
- Use
Runtime.getRuntime().exec()
and callecho
program and the list follows.
+1 to @eis
You can use the JDK's logger (java.util.logging.Logger).
This is how you create a logger in your java class.
import java.util.logging.Logger;
private final static Logger LOGGER = Logger.getLogger(MyClass.class .getName());
Also You could use log4j for the same purpose.