JAVA IO修改文本内容

使用JD-GUI反编译Jar源码的时候,点击Save all source,保存为zip包,而包中的源码每行都多出了注释/** */ 标记,所以下面的代码则为去除每行的注释标记;

如下:

package com.htjc.netpay;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;

public class FileTest {
    /**
     * 
     * 删除文本中所有的 \*   *\  注释标记
     * @param oldStr 
     *            查找内容 
     * @param replaceStr 
     *            替换内容 
     */ 
    public static void replaceTxtBy(File file) {
    	System.out.println("扫描文件:"+file.getPath()+" 所有注释");
        String temp = ""; 
        try {
            FileInputStream fis = new FileInputStream(file); 
            InputStreamReader isr = new InputStreamReader(fis,"utf-8"); 
            BufferedReader br = new BufferedReader(isr); 
            StringBuffer buf = new StringBuffer();
            int b = -1;
            int a = -1;
            // 保存该行前面的内容
            boolean falg =false;
            while ((temp = br.readLine()) != null) {
            	a = temp.indexOf("/*");
            	if(a!=-1){
            		falg = true;
            	}
            	b = temp.indexOf("*/");
            	if(falg && b!=-1){
            		temp = temp.substring(b+2);
            		falg = false;
            		b = -1;
            	}
            	if(!falg){
            		buf.append(temp).append(System.getProperty("line.separator"));
            	}
            }
            br.close();
            FileOutputStream fos = new FileOutputStream(file); 
            PrintWriter pw = new PrintWriter(fos); 
            pw.write(buf.toString().toCharArray());
            pw.flush(); 
            pw.close(); 
        } catch (IOException e) {
            e.printStackTrace(); 
        }
    }
    
    public static void scanFile(String path){
    	File f = new File(path);
    	if(f.isDirectory()){
    		File[] files = f.listFiles();
    		for(File fis :files){
    			scanFile(fis.getPath());			
    		}
    	}else{
    		if(f.getName().indexOf(".java")!=-1){
				replaceTxtBy(f);
			}
    	}
    }
    
    
    /**
     * main方法测试 
     * @param s 
     * @throws IOException 
     */ 
    public static void main(String[] s) throws IOException { 
    	scanFile("D:\\fdfs");
    } 
}



转载于:https://my.oschina.net/woter/blog/883512

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值