I want to remove/comment all the occurrences of System.out.println
from my java code.
This System.out.println
may be inside if
,
Update
As mentioned here create a NullOutPut
public final DevNull {
public final static PrintStream out = new PrintStream(new OutputStream() {
public void close() {}
public void flush() {}
public void write(byte[] b) {}
public void write(byte[] b, int off, int len) {}
public void write(int b) {}
} );
}
and replace System.out.print
with DevNull.out.print
and later switch to logging framework that will allow you to handle stuff easily
Linked
You could use raw find/replace functionality in source code files to comment out (or remove) statements, but whatever regular expression can be easily broken, so maybe you'd better be aware of logging frameworks like log4j or slf4j.
Big picture first: instead of using the usual System.out.println()
, you'll end up using a line of code like:
logger.debug("Just entered main");
The logging framework can be configured with a simple property file, so you can have multiple appenders (console, file, database, whatever) and shut down each one separately on demand (for example the console appender). To switch to a logging API you still have to perform raw find/replace on source files, and possibly fix a couple of things by hand, either whithin your IDE, or with a command like:
find src/ -name '*.java' | \
xargs sed -i -e 's/System.out.println/logger.verbose/g'
As Jigar Joshi said, using find & replace approach might help you a lot, especially if you don't interfere the code apart from System.out.println
.
I would propose a different solution if editing/changing the source code is not MUST for you. You can disable the System.out
stream in your driver program, then nothing will be printed by these statements. You only need to set it like this:
System.setOut(new PrintStream(new OutputStream() {
public void write(int b) {
// No operation.
}
public void close() {}
public void flush() {}
public void write(byte[] b) {}
public void write(byte[] b, int off, int len) {}
public void write(int b) {}
}));
Do a simple find and replace. That should do.
In case you do not use IDE, on Linux:
sed '/System.out.println/d' %YOUR_PROJ_DIR%/*.java
on Windows you can do the same if you have a cygwin installed.
You can make all System.out.println
in your application not to print anything in console. Create a class like:
import java.io.PrintStream;
public class MyStream extends PrintStream {
private static final MyStream INSTANCE = new MyStream();
public static void init() {
System.setOut(INSTANCE);
}
private MyStream() {
super(System.out);
}
@Override
public void println(Object x) {
return;
}
@Override
public void println(String x) {
return;
}
}
Use it as:
public class Test {
public static void main(String... args) {
MyStream.init();
System.out.println("This line will not print");
}
}
The will not be printed anymore.