private static final int ISDIR = 0; private static final int ISCOMFILE = 1; private static final int ISRPFILE = 2;
private static int replaceFile(File curFile){ System.out.print("\ndeal File:" + curFile.getAbsolutePath()); int fileType = getType(curFile); int result = 0; System.out.println(" File Type:" + fileType); switch(fileType){ case ISDIR: result = makeDir(curFile); break; case ISCOMFILE: result = copyFile(curFile); break; case ISRPFILE: result = replaceCopyFile(curFile); break; }
File[] subFile = curFile.listFiles();
if (subFile != null){ for(int i = 0 ;i < subFile.length ; i++){ result += replaceFile(subFile[i]); } }
return result; }
private static int getType(File curFile){ if (curFile == null){ System.out.println("err! founde a null object when handle input parameter(curFile) in function getType"); return -1; }
if (curFile.isDirectory()){ return ISDIR; }
//then just if the file name is match the partten String curFileName = curFile.getName(); boolean isWaitUntilChar = false; boolean isMatch = true; //System.out.println("<<<<<<<<<<<<curFileName>>>>>>>>>>>:"+curFileName); //System.out.println("<<<<<<<<<<<<partten>>>>>>>>>>>:"+partten); int k = -1;//this is the index for curFileName chars for (int i = 0 ; i < partten.length() ; i++){ if (!isMatch){ break; } char parttenChar = partten.charAt(i); if (parttenChar == '*'){ isWaitUntilChar = true; continue; } for (k++; k < curFileName.length() ; k++){ //System.out.println("i,k:"+i+" " + k); if (isWaitUntilChar){ if(curFileName.charAt(k) == parttenChar){ isWaitUntilChar = false; break; } continue; } else{ if(curFileName.charAt(k) != parttenChar){ //misMatch isMatch = false; } break; } } }
if (isMatch){ return ISRPFILE; }
return ISCOMFILE; }
private static int copyFile(File curFile){ System.out.println("copy >>>" + curFile.getName()); byte[] buffer = new byte[1024]; InputStream curFileStream = null; OutputStream descFileStream = null; int length = -1; try{ curFileStream = new BufferedInputStream(new FileInputStream(curFile)); descFileStream = new BufferedOutputStream(new FileOutputStream(getDestFile(curFile))); while((length = curFileStream.read(buffer)) != -1){ descFileStream.write(buffer,0,length); } curFileStream.close(); descFileStream.close(); return 0; }catch(Exception e){ System.out.println("error when copy " + curFile.getName() + " to " + getDestFile(curFile).getName()); System.out.println(e); return 1; } }
private static int makeDir(File curFile){ System.out.println("making dir >>>" + curFile.getName()); try{ return getDestFile(curFile).mkdirs()?0:100; }catch(Exception e){ System.out.println("error when create direct:" + curFile.getName()); System.out.println(e); return 100; } }
public static void main(String[] args){ if (args.length !=3){ System.out.println("java Replace <orgString> <descString> <filePattern>"); System.out.println("it will use <descString> to replace the String <oreString> in file that"); System.out.println("in the current Direct and it's SubDirect"); System.out.println("event if the fileName is Match with the <filePattern>"); System.out.println("the replaceed file and not match files will put to the new Direct[output] with same struct"); System.out.println("example:"); System.out.println("java Replace 10.70.116.54 192.168.11.6 *.jad"); return; }
baseDir = (new File(".").getAbsolutePath()).substring(0,(new File(".").getAbsolutePath()).length()-3);
File currentDir = new File("."); baseDir = currentDir.getAbsolutePath().substring(0,currentDir.getAbsolutePath().length()-2);
还有一个简单的方法: 写一个类: class MyOutput extends OutputStream { private final OutputStream o; public MyOutput { System sys = null; o = sys.out; } public int write(int b)throws java.io.IOException { //o.write(b); } public static final java.io.PrintStream out = new java.io.PrintStream(new MyOutput,true); } 然后全局替换 System.out 成 MyOutput.out. 这样就可以屏蔽System.out了,如果想恢复,把o.write(b)注释去掉就可以了。
不光删System.out.println,还要删System.out.println 后面的(”。“)内容呀。
如果System.out.println语句后面还有语句就不能用”//“了。
首先,
查找(Find):System.out.println(" 内容不限 ")?
然后,
全部替换(replace all)://System.out.println(" 内容不限 ")?
帮你顶一下,让其他人帮帮忙吧
你都能找到那了就可以自己手动删嘛
我的意思是先定位到每一个System......
用/* System.out.println(" 内容不限 ");*/
我曾经用这个来面试,结果可惜,没有几个人能做出来,我也觉的很纳闷呢,觉得这个程序很简单啊,怎么就没几个能搞定的?import java.io.*;public class Replace{
private static String partten;
private static String orgString;
private static String descString;
private static String baseDir;
private static String descDir;
private static final int ISDIR = 0;
private static final int ISCOMFILE = 1;
private static final int ISRPFILE = 2;
private static int replaceFile(File curFile){
System.out.print("\ndeal File:" + curFile.getAbsolutePath());
int fileType = getType(curFile);
int result = 0;
System.out.println(" File Type:" + fileType);
switch(fileType){
case ISDIR:
result = makeDir(curFile);
break;
case ISCOMFILE:
result = copyFile(curFile);
break;
case ISRPFILE:
result = replaceCopyFile(curFile);
break;
}
File[] subFile = curFile.listFiles();
if (subFile != null){
for(int i = 0 ;i < subFile.length ; i++){
result += replaceFile(subFile[i]);
}
}
return result;
}
private static int getType(File curFile){
if (curFile == null){
System.out.println("err! founde a null object when handle input parameter(curFile) in function getType");
return -1;
}
if (curFile.isDirectory()){
return ISDIR;
}
//then just if the file name is match the partten
String curFileName = curFile.getName();
boolean isWaitUntilChar = false;
boolean isMatch = true;
//System.out.println("<<<<<<<<<<<<curFileName>>>>>>>>>>>:"+curFileName);
//System.out.println("<<<<<<<<<<<<partten>>>>>>>>>>>:"+partten);
int k = -1;//this is the index for curFileName chars
for (int i = 0 ; i < partten.length() ; i++){
if (!isMatch){
break;
} char parttenChar = partten.charAt(i);
if (parttenChar == '*'){
isWaitUntilChar = true;
continue;
}
for (k++; k < curFileName.length() ; k++){
//System.out.println("i,k:"+i+" " + k);
if (isWaitUntilChar){
if(curFileName.charAt(k) == parttenChar){
isWaitUntilChar = false;
break;
}
continue;
}
else{
if(curFileName.charAt(k) != parttenChar){
//misMatch
isMatch = false;
}
break;
}
}
}
if (isMatch){
return ISRPFILE;
}
return ISCOMFILE;
}
private static int copyFile(File curFile){
System.out.println("copy >>>" + curFile.getName());
byte[] buffer = new byte[1024];
InputStream curFileStream = null;
OutputStream descFileStream = null;
int length = -1;
try{
curFileStream = new BufferedInputStream(new FileInputStream(curFile));
descFileStream = new BufferedOutputStream(new FileOutputStream(getDestFile(curFile)));
while((length = curFileStream.read(buffer)) != -1){
descFileStream.write(buffer,0,length);
}
curFileStream.close();
descFileStream.close();
return 0;
}catch(Exception e){
System.out.println("error when copy " + curFile.getName() + " to " + getDestFile(curFile).getName());
System.out.println(e);
return 1;
}
}
private static int makeDir(File curFile){
System.out.println("making dir >>>" + curFile.getName());
try{
return getDestFile(curFile).mkdirs()?0:100;
}catch(Exception e){
System.out.println("error when create direct:" + curFile.getName());
System.out.println(e);
return 100;
}
}
private static int replaceCopyFile(File curFile){
System.out.println("replace File >>>" + curFile.getName());
String curLine;
BufferedReader curFileReader = null;
BufferedWriter descFileWriter = null;
try{
curFileReader = new BufferedReader(new InputStreamReader(new FileInputStream(curFile)));
descFileWriter = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(getDestFile(curFile))));
while((curLine = curFileReader.readLine()) != null){
descFileWriter.write(replaceStr(curLine));
descFileWriter.write("\r\n");
}
curFileReader.close();
descFileWriter.close();
return 0;
}catch(Exception e){
System.out.println("error when replace File " + curFile.getName() + " to " + getDestFile(curFile).getName());
System.out.println(e); return 10000;
}
}
private static String replaceStr(String srcLine){
if (srcLine == null)
return null;
int prossion = srcLine.indexOf(orgString);
if (prossion == -1){
return srcLine;
} return srcLine.substring(0,prossion) + descString + srcLine.substring(prossion + orgString.length(),srcLine.length());
}
private static File getDestFile(File curFile){
String allPath = curFile.getAbsolutePath();
String newPath = descDir + allPath.substring(baseDir.length(),allPath.length());
System.out.println("new dir is:" + newPath);
return new File(newPath);
}
public static void main(String[] args){
if (args.length !=3){
System.out.println("java Replace <orgString> <descString> <filePattern>");
System.out.println("it will use <descString> to replace the String <oreString> in file that");
System.out.println("in the current Direct and it's SubDirect");
System.out.println("event if the fileName is Match with the <filePattern>");
System.out.println("the replaceed file and not match files will put to the new Direct[output] with same struct");
System.out.println("example:");
System.out.println("java Replace 10.70.116.54 192.168.11.6 *.jad");
return;
}
baseDir = (new File(".").getAbsolutePath()).substring(0,(new File(".").getAbsolutePath()).length()-3);
File currentDir = new File(".");
baseDir = currentDir.getAbsolutePath().substring(0,currentDir.getAbsolutePath().length()-2);
System.out.println("baseDir : " + baseDir);
descDir = baseDir + "/output";
System.out.println("descDir : " + descDir);
System.out.println("..............................................");
orgString = args[0];
descString = args[1];
partten = args[2];
currentDir = new File(baseDir);
File[] filelist = currentDir.listFiles();
for(int i = 0 ; i < filelist.length ;i++){
if (!"output".equals(filelist[i].getName())){
replaceFile(filelist[i]);
}
}
}
}
变通一下嘛!你不会将
System.out.printl(
替换成
//System.out.printl(
这样不就行了!
“//System...”替换行得通吗?
请问,下面的怎样替换?
System.out.printl("AAAAAAAAAAAAAAA" + Integer.toString(i * 3.1415926 + (J - K))
+ "BBBBBBB" + strABC + ":"
+ "CCCCCCCC");只能通过编程+源文件语法分析实现
import java.io.*;public class DelSysout{
public static void main(String a[]){
String s="";
int ptr=0;
try{
File filein=new File("hasout.java");
File fileout=new File("noout.java");
FileReader file_reader=new FileReader(filein);
BufferedReader in=new BufferedReader(file_reader);
FileWriter file_writer=new FileWriter(fileout);
BufferedWriter out=new BufferedWriter(file_writer);
while((s=in.readLine())!=null)
{
//先判断有无System.out.print
if(s.indexOf("System.out.print")>=0){
//再判断“;”,循环用于删除多行的情况
if(s.indexOf(";")<0){
while((s=in.readLine())!=null&&s.indexOf(";")<0);
}else{
//如果后面还有语句,就这里处理
}
}else{
out.newLine();
out.write(s);
}
}
in.close();
file_reader.close();
out.close();
file_writer.close();
}catch(Exception e1){
e1.printStackTrace();
}
}
}
一共花不了10秒钟
写一个类:
class MyOutput extends OutputStream
{
private final OutputStream o;
public MyOutput
{
System sys = null;
o = sys.out;
}
public int write(int b)throws java.io.IOException
{
//o.write(b);
}
public static final java.io.PrintStream out = new java.io.PrintStream(new MyOutput,true);
}
然后全局替换 System.out 成 MyOutput.out.
这样就可以屏蔽System.out了,如果想恢复,把o.write(b)注释去掉就可以了。
"System.out.println("
替换成
"//System.out.println("
搞定