如何一次性删除所有的system.out.println语句 是用perl ,ant, 或find grep? 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 这个有意思。一般都用 replaceWith 单个文件中批量删除。 eclipse ,选中项目 ,ctrl+h ,选 file search, 输入 System.out.print(?) 表达式,勾选正则表达式选项, 按 replace 。有这个功能,但是我没用过,你可以试试。 一般用replace 把 system.out.println语句 全部替换成 空 嘿嘿,工程开始的时候,做好debug方法,现在就不用这么头疼了。 用excel会很简单……用excelVBA更简单 import java.io.BufferedReader;import java.io.FileReader;import java.io.FileWriter;public class DeleteString { /** * @param args * @throws Exception */ public static void main(String[] args) throws Exception { // TODO Auto-generated method stub FileReader fr = new FileReader("c:\\111.java"); BufferedReader br = new BufferedReader(fr); FileWriter fw = new FileWriter("c:\\22.java"); while (br.ready()) { String str = br.readLine() + "\r\n"; if (str.indexOf("System.out.print") < 0) { fw.write(str); } } fw.flush(); fr.close(); fw.close(); }} 定义一个空函数 MyDebug.println然后用批量替换吧system.out.println换成MyDebug.println 一般遇到这种情况我都死查找替换~!ps:<ctrl + h > 出现查找对话框 在对话框的下面有三个按钮“replace”,“search”,“cancel”点“replace”按钮将system.out.println 替换长“” 直接用dreamweaver 把system.out.println替换为//system.out.println 办法很多,替换就成,Eclipse之类的ide都有这功能,至少word也能。还是在开始做项目的时候用debug框架吧,log4j之类的东西。这样就方便多了。下次想再让他们显示也方便。 我们或许可以换种角度去思考,把 System.out 的输出流重定向到 log4j 或者其他日志中去:log4j.xml 配置(放到 src 下,即 classpath 下):<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"><log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%d %-5p %t [%c{1}] %m%n" /> </layout> </appender> <logger name="SystemOut"> <level value="DEBUG" /> <appender-ref ref="CONSOLE" /> </logger> <root> <priority value="INFO" /> </root></log4j:configuration>测试类:public class SystemOutLog { static { Log4jPrintStream.redirectSystemOut(); } public static void main(String[] args) { for(int i = 0; i < 10; i++) { System.out.print("abc"); System.out.print(i); System.out.print((char)(i + 0x21)); } }}Log4jPrintStream 的实现:import java.io.IOException;import java.io.OutputStream;import java.io.PrintStream;import java.util.Locale;import org.apache.log4j.Logger;public class Log4jPrintStream extends PrintStream { private Logger log = Logger.getLogger("SystemOut"); private static PrintStream instance = new Log4jPrintStream(System.out); private Log4jPrintStream(OutputStream out) { super(out); } public static void redirectSystemOut() { System.setOut(instance); } public PrintStream append(char c) { // Ignore return this; } public PrintStream append(CharSequence csq, int start, int end) { // Ignore return this; } public PrintStream append(CharSequence csq) { // Ignore return this; } public boolean checkError() { // Ignore return false; } protected void clearError() { // Ignore } public void close() { // Ignore } public void flush() { // Ignore } public PrintStream format(Locale l, String format, Object... args) { // Ignore return this; } public PrintStream format(String format, Object... args) { // Ignore return this; } public void print(boolean b) { println(b); } public void print(char c) { println(c); } public void print(char[] s) { println(s); } public void print(double d) { println(d); } public void print(float f) { println(f); } public void print(int i) { println(i); } public void print(long l) { println(l); } public void print(Object obj) { println(obj); } public void print(String s) { println(s); } public PrintStream printf(Locale l, String format, Object... args) { // Ignore return this; } public PrintStream printf(String format, Object... args) { // Ignore return this; } public void println() { // Ignore } public void println(boolean x) { log.debug(Boolean.valueOf(x)); } public void println(char x) { log.debug(Character.valueOf(x)); } public void println(char[] x) { log.debug(x == null ? null : new String(x)); } public void println(double x) { log.debug(Double.valueOf(x)); } public void println(float x) { log.debug(Float.valueOf(x)); } public void println(int x) { log.debug(Integer.valueOf(x)); } public void println(long x) { log.debug(x); } public void println(Object x) { log.debug(x); } public void println(String x) { log.debug(x); } protected void setError() { // Ignore } public void write(byte[] buf, int off, int len) { // Ignore } public void write(int b) { // Ignore } public void write(byte[] b) throws IOException { // Ignore }}把 System.out 的输出流重定向到 log4j 的 DEBUG 级别日志中去 为了提高效率,还能改成这样: public void println(boolean x) { if(log.isDebugEnabled()) { log.debug(Boolean.valueOf(x)); } } public void println(char x) { if(log.isDebugEnabled()) { log.debug(Character.valueOf(x)); } } public void println(char[] x) { if(log.isDebugEnabled()) { log.debug(x == null ? null : new String(x)); } } public void println(double x) { if(log.isDebugEnabled()) { log.debug(Double.valueOf(x)); } } public void println(float x) { if(log.isDebugEnabled()) { log.debug(Float.valueOf(x)); } } public void println(int x) { if(log.isDebugEnabled()) { log.debug(Integer.valueOf(x)); } } public void println(long x) { if(log.isDebugEnabled()) { log.debug(Long.valueOf(x)); } } 用editplus或者ultraedit都可以批量替换所有文件里面的 我一般自己写个print工具集来调用,不直接写System.out.println,便于在需要时统一关掉它或重定向。 long time ago ..... 建议 System.out.pr全部替换成// 每天回帖即可获得10分可用分!小技巧:教您如何更快获得可用分 这里发言,表示您接受了CSDN社区的用户行为准则。 请对您的言行负责,并遵守中华人民共和国有关法律法规,尊重网上道德。 转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。 ctrl+h查询一下所有的,在替换一下。 jacob 解析word的问题 sha2中有224 256 384 512 一般选择哪个 理由是什么? 数据库操作commit失败后,需要再rollback吗? 求助:weblogic jndi 绑定自定义类的实例问题 JS生成的文本框如何用Struts从JSP上面去得值 javax.crypto.Cipher;问题 struts+hibernate的问题 超一流难题,新手莫入,关于Weblogic中打开word之类文档显示乱码 Jboss3 + EJB如何指定EntityBean所用的数据库连接池 初学者 大家帮忙看看这个我应该怎么转? 伙计们,紧急求助啊。。。。。。
用excelVBA更简单
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;public class DeleteString { /**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
FileReader fr = new FileReader("c:\\111.java");
BufferedReader br = new BufferedReader(fr);
FileWriter fw = new FileWriter("c:\\22.java");
while (br.ready()) {
String str = br.readLine() + "\r\n";
if (str.indexOf("System.out.print") < 0) {
fw.write(str);
}
}
fw.flush();
fr.close();
fw.close();
}}
然后用批量替换吧system.out.println换成MyDebug.println
ps:<ctrl + h > 出现查找对话框 在对话框的下面有三个按钮“replace”,“search”,“cancel”
点“replace”按钮将system.out.println 替换长“”
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p %t [%c{1}] %m%n" />
</layout>
</appender>
<logger name="SystemOut">
<level value="DEBUG" />
<appender-ref ref="CONSOLE" />
</logger>
<root>
<priority value="INFO" />
</root>
</log4j:configuration>
测试类:public class SystemOutLog {
static {
Log4jPrintStream.redirectSystemOut();
} public static void main(String[] args) {
for(int i = 0; i < 10; i++) {
System.out.print("abc");
System.out.print(i);
System.out.print((char)(i + 0x21));
}
}
}Log4jPrintStream 的实现:
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.util.Locale;import org.apache.log4j.Logger;public class Log4jPrintStream extends PrintStream { private Logger log = Logger.getLogger("SystemOut"); private static PrintStream instance = new Log4jPrintStream(System.out); private Log4jPrintStream(OutputStream out) {
super(out);
} public static void redirectSystemOut() {
System.setOut(instance);
} public PrintStream append(char c) {
// Ignore
return this;
} public PrintStream append(CharSequence csq, int start, int end) {
// Ignore
return this;
} public PrintStream append(CharSequence csq) {
// Ignore
return this;
} public boolean checkError() {
// Ignore
return false;
} protected void clearError() {
// Ignore
} public void close() {
// Ignore
} public void flush() {
// Ignore
} public PrintStream format(Locale l, String format, Object... args) {
// Ignore
return this;
} public PrintStream format(String format, Object... args) {
// Ignore
return this;
} public void print(boolean b) {
println(b);
} public void print(char c) {
println(c);
} public void print(char[] s) {
println(s);
} public void print(double d) {
println(d);
} public void print(float f) {
println(f);
} public void print(int i) {
println(i);
} public void print(long l) {
println(l);
} public void print(Object obj) {
println(obj);
} public void print(String s) {
println(s);
} public PrintStream printf(Locale l, String format, Object... args) {
// Ignore
return this;
} public PrintStream printf(String format, Object... args) {
// Ignore
return this;
} public void println() {
// Ignore
} public void println(boolean x) {
log.debug(Boolean.valueOf(x));
} public void println(char x) {
log.debug(Character.valueOf(x));
} public void println(char[] x) {
log.debug(x == null ? null : new String(x));
} public void println(double x) {
log.debug(Double.valueOf(x));
} public void println(float x) {
log.debug(Float.valueOf(x));
} public void println(int x) {
log.debug(Integer.valueOf(x));
} public void println(long x) {
log.debug(x);
} public void println(Object x) {
log.debug(x);
} public void println(String x) {
log.debug(x);
} protected void setError() {
// Ignore
} public void write(byte[] buf, int off, int len) {
// Ignore
} public void write(int b) {
// Ignore
} public void write(byte[] b) throws IOException {
// Ignore
}
}把 System.out 的输出流重定向到 log4j 的 DEBUG 级别日志中去
if(log.isDebugEnabled()) {
log.debug(Boolean.valueOf(x));
}
} public void println(char x) {
if(log.isDebugEnabled()) {
log.debug(Character.valueOf(x));
}
} public void println(char[] x) {
if(log.isDebugEnabled()) {
log.debug(x == null ? null : new String(x));
}
} public void println(double x) {
if(log.isDebugEnabled()) {
log.debug(Double.valueOf(x));
}
} public void println(float x) {
if(log.isDebugEnabled()) {
log.debug(Float.valueOf(x));
}
} public void println(int x) {
if(log.isDebugEnabled()) {
log.debug(Integer.valueOf(x));
}
} public void println(long x) {
if(log.isDebugEnabled()) {
log.debug(Long.valueOf(x));
}
}
...
这里发言,表示您接受了CSDN社区的用户行为准则。
请对您的言行负责,并遵守中华人民共和国有关法律法规,尊重网上道德。
转载文章请注明出自“CSDN(www.csdn.net)”。如是商业用途请联系原作者。