遍历处理文件夹下所有.java文件代码左侧含有形如 /* 56 */注释的代码,并存到新的目录下

本文介绍了一款用于处理包含特殊格式注释(如 /*数字*/)的Java文件的工具。该工具能够遍历指定目录下的所有Java文件,移除这类注释,并将处理后的文件保存到新的目录中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


遍历处理文件夹下所有.java文件代码左侧含有形如 /* 56 */注释的代码,并存到新的目录下


package until;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 
 * 程序会自动遍历所有文件夹下的.java文件并进行处理 
 * @author 
 *
 */
public class FileManager {

	public static HashMap<Integer, String> map;

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		/**
		 * 存放反编译好的代码
		 */
		File file = new File("D:/01/");

		/**
		 * 程序自动创建并存放处理好的代码
		 */
		File file2 = new File("D:/02/");

		getCode(file, file2);

	}

	//遍历dir目录下所有的.java文件名并处理格式注释的代码如右侧代码 /* 56 */ public void ....
	//存储新的代码到newCodeDir下
	public static void getCode(File dir, File newCodeDir) {

		dir.mkdirs();
		newCodeDir.mkdirs();
		File[] files = dir.listFiles();
		String cmd = "";
		int index = 0;
		String xmlPath = newCodeDir.getAbsolutePath() + "/";
		xmlPath = xmlPath.replace("\\", "/");
		newCodeDir.mkdirs();

		Pattern p = Pattern.compile("/\\*[ \\d\\w]+\\*/");

		for (int i = 0; i < files.length; i++) {
			if (!files[i].isDirectory() && files[i].getName().endsWith(".java")) {
				File file = files[i];
				String path = file.getPath();
				String name = file.getName();
				FileInputStream fis = null;
				InputStreamReader isr = null;
				BufferedReader br = null;
				FileOutputStream fos = null;
				DataOutputStream dos = null;

				try {
					fis = new FileInputStream(file);
					isr = new InputStreamReader(fis, "UTF-8");
					br = new BufferedReader(isr);
					new File(xmlPath).mkdirs();
					fos = new FileOutputStream(new File(xmlPath + name), true);
					System.out.println(xmlPath + name);
					dos = new DataOutputStream(fos);
					String temp = null;
					while ((temp = br.readLine()) != null) {
						Matcher m = p.matcher(temp);
						while (m.find()) {
							String str = m.group();
							temp = temp.replace(str, " ");
						}
						temp = temp + "\r\n";
						dos.write(temp.getBytes("utf-8"));
					}
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				} finally {
					try {
						if (dos != null) {
							dos.close();
						}
						if (fos != null) {
							fos.close();
						}
						if (br != null) {
							br.close();
						}
						if (isr != null) {
							isr.close();
						}
						if (fis != null) {
							fis.close();
						}
					} catch (Exception e) {
						e.printStackTrace();
					}
				}
			} else if (files[i].isDirectory()) {
				String path = newCodeDir.getAbsolutePath() + "/"
						+ files[i].getName() + "/";
				path = path.replace("\\", "/");
				getCode(files[i], new File(path));
			}
		}
	}

}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值