删除某个目录下js文件中的注释

import java.io.BufferedReader;   
import java.io.BufferedWriter;
import java.io.File;   
import java.io.FileReader;   
import java.io.FileWriter;
import java.util.Scanner;
  
/**  
 * @author winnie  
 *   
 */  
public class DelCommentsInJava {   
  
    private static final char SLASH = '/';   
  
    private static final char STAR = '*';   
  
    private static final char NEWLINE = '\n';   
       
    //斜杠   
    private static final int TYPE_SLASH = 1;   
       
    //星号   
    private static final int TYPE_STAR = 2;   
  
    // 双斜杠类型的注释   
    private static final int TYPE_DSLASH = 3;  
    
   //  /*的注释
    private static final int TYPE_STAR_SLASH = 4;   
  
    public static char[] del(char[] _target, int _start, int _end) {   
        char[] tmp = new char[_target.length - (_end - _start + 1)];   
        System.arraycopy(_target, 0, tmp, 0, _start);   
        System.arraycopy(_target, _end + 1, tmp, _start, _target.length - _end   
                - 1);   
        return tmp;   
    }   
  
    public static String delComments(String _target) {   
        int preType = 0;   
        int cur = -1, token = -1;   
        char[] input =  _target.toCharArray();   
        for (cur = 0; cur < input.length; cur++) {   
            if (input[cur] == SLASH) {   
                if (preType == TYPE_STAR_SLASH) {   
                	if(input[cur-1] == STAR)
                	{
                		input = del(input, token, cur);   
                        cur = token - 1;   
                        preType = 0;  
                	}else {
                		preType = TYPE_STAR_SLASH;   
                	}
                } else if (preType == TYPE_SLASH) {   
                	token = cur - 1;   
                	if(input[token] == SLASH)
                	{
                		//token = cur - 1;   
                		preType = TYPE_DSLASH;   
                	}
                } else if (preType == TYPE_DSLASH){   
                    preType = TYPE_DSLASH;   
                } else {
                	preType = TYPE_SLASH;   
                }
            } else if (input[cur] == STAR) {   
            	if(preType == TYPE_STAR_SLASH){
            		preType = TYPE_STAR_SLASH;   
            	}
            	else if (preType == TYPE_SLASH) {
                	if(input[cur-1] == SLASH){
                		token = cur - 1;   
                        preType = TYPE_STAR_SLASH;   
                	}else{
                		preType = TYPE_STAR;   
                	}
                }else if  (preType == TYPE_DSLASH) {
                	preType = TYPE_DSLASH;   
                } else{
                	preType = TYPE_STAR;   
                }
                
            } else if(input[cur] == NEWLINE)   
            	{   
                	if(preType == TYPE_DSLASH)   
                	{   
                		input = del(input, token, cur-1);   
                		cur = token - 1;   
                		preType = 0;   
                	}   
            	}   
  
        	}   
        return new String(input);   
    }   
  
    public static void main(String[] args) {   
        try {   
            Scanner sc =new Scanner(System.in);
        	System.out.println("输入目录:");
        	String dirname = sc.next();
        	File dir = new File(dirname);   
        	File[]   listfile   =   dir.listFiles();  
        	for(int i = 0;i<listfile.length;i++)
        	{
        		BufferedReader reader = new BufferedReader(new FileReader(listfile[i]));   
                StringBuilder content = new StringBuilder();   
                String tmp = null;   
                while ((tmp = reader.readLine()) != null) {   
                    content.append(tmp);   
                    content.append("\n");   
                }   
                String target = content.toString();   
                String target1 = delComments(target); 
                File f = new File(listfile[i]+".bak");
                BufferedWriter outputbak = new BufferedWriter(new FileWriter(f));
                outputbak.write(target);
                outputbak.close();
                BufferedWriter output = new BufferedWriter(new FileWriter(listfile[i]));
                output.write(target1);
                output.close();
        	}
        } catch (Exception e) {   
  
        }   
    }
}  

你可能感兴趣的:(java,F#,idea)