Recently one of my friend had following requirement. I guess it can be useful to anyone.
In case you want to redirect all the System.out.println() output to some file, you can do using following methods provided by System class.
System.setOut(), System.setIn() and System.setErr()
So, to redirect the output and error logs, you can simply say
Sometimes its required to hide all System.out.println outputs when the existing application is unnecessarily writing lot of outputs filling the system space and it would take some time to modify all the code. To do this, you can subclass PrintStream and override its methods for no-op.
In case you want to redirect all the System.out.println() output to some file, you can do using following methods provided by System class.
System.setOut(), System.setIn() and System.setErr()
So, to redirect the output and error logs, you can simply say
String outFile = "/tmp/sysout.log";
System.setOut(new PrintStream(new FileOutputStream(outFile)));
String errFile = "/tmp/syserr.log";
System.setOut(new PrintStream(new FileOutputStream(errFile)));
Sometimes its required to hide all System.out.println outputs when the existing application is unnecessarily writing lot of outputs filling the system space and it would take some time to modify all the code. To do this, you can subclass PrintStream and override its methods for no-op.
System.out.println("Hello, program started.");
String filename = "D:\\sysout.log"; //or put some dummy filepath, need to this to construct PrintStream
System.setOut(new PrintStream(new FileOutputStream(filename)) {
@Override
public void print(String paramString) {
//do nothing
}
@Override
public void println(String paramString) {
//do nothing
}
//above will just hide the output for print(String) and println(String) methods.
//override all other print/println methods so nothing gets printed at all.
}
);
System.out.println("This is another string in sysout.");
Great Article. Thank you for sharing! Really an awesome post for every one.
ReplyDeleteMobile Encrypted Traffic Classification Using Deep Learning Experimental Evaluation,Lessons Learned, and Challenges Project For CSE
Online Learning based Down link Transmission Coordination in Ultra Dense mm Wave Heterogeneous Networks Project For CSE
You’re Not Acting Like Yourself A Study on Soft Biometric Classification, Person Identification,and Mobile Device Use Project For CSE
A Combination Method for Android Malware Detection Based on Control Flow Graphs and Machine Learning Algorithms Project For CSE
A Thread Oriented Memory Framework for Mobile Edge Computing Project For CSE
An Indoor Position Estimation Algorithm Using Smartphone IMU Sensor Data Project For CSE