`
53873039oycg
  • 浏览: 837115 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

Java应用之删除Java文件Main方法改进

    博客分类:
  • java
 
阅读更多

     上午写的那篇去除Main方法由于本人考虑欠缺,以为一个Java类里面只有一个Main方法,没有考虑到被注释的Main方法,所以可能导致去除的是被注释的Main方法,下午,重新写了个,代码和上午的差不多,主要是把处理逻辑放在了while循环中。

      代码如下:

     

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class 删除全部Main方法 {
	public static void main(String[] args) throws Exception {
		删除全部Main方法 t = new 删除全部Main方法();
		String content = t.getFileContent("F:/saveFile/tmp/c/SimpleTest.java",
				"utf-8");
		String result = t.delMainMethod(content);
		t.writeStrToFile(result, "F:/saveFile/tmp/c/SimpleTest.java", "utf-8");
	}

	public String delMainMethod(String content) {
		int mainStart = -1;
		int checkStart = -1;
		String tmpStr= null;
		String tmpContent = new String(content);
		StringBuffer tmpBuffer = new StringBuffer(5120);
		boolean flag=false;
		// 得到真正的main方法
		while ((mainStart = tmpContent.lastIndexOf("main")) != -1) {
			tmpStr = new String(tmpContent.substring(mainStart));
			checkStart = tmpStr.indexOf(")");
			tmpStr = new String(tmpContent.substring(mainStart - 1, mainStart
					+ checkStart + 1));
			tmpStr = tmpStr.replaceAll("#_end_#", "\n");
			tmpStr = tmpStr.replaceAll("\\n", "");
			tmpStr = tmpStr.replaceAll("\\s+", "");
			if (tmpStr.startsWith("main(String") && (tmpStr.indexOf(",") == -1)) {
				tmpContent=new String(processMain(tmpContent,mainStart));
				flag=true;
			}else{
				//截取前半部分,后半部分丢弃,所以要保留下来
				tmpBuffer.insert(0,new String(tmpContent.substring(mainStart)));
				tmpContent = new String(tmpContent.substring(0, mainStart));
			}
		}
		if (!flag) {
			// 去掉多余空白行
			content = new String(content.replaceAll("#_end_#", "\r\n"));
			content = new String(content.replaceAll("\\s{2,}\\r\\n", "\r\n"));
			return content;
		}
		tmpBuffer.insert(0,tmpContent);
		tmpContent=null;
		tmpContent=tmpBuffer.toString();
		// 去掉多余空白行
		tmpContent = new String(tmpContent.replaceAll("#_end_#", "\r\n"));
		//去掉///* * */
		tmpContent = new String(tmpContent.replaceAll("/\\*(\\s*\\*\\s*\\s*?)*\\*\\/", ""));
		//去掉/* */
		tmpContent = new String(tmpContent.replaceAll("\\/\\*\\s*\\*\\/", ""));
		tmpContent = new String(tmpContent.replaceAll("\\s{2,}\\r\\n", "\r\n"));
		return tmpContent;
	}

	public String processMain(String content, int mainStart) {
		int publicStart = -1;
		String tmp = null;
		String tmp2=new String(content);
		if (mainStart != -1) {
			tmp = new String(tmp2.substring(0, mainStart));
			// 得到main方法的开始位置
			publicStart = tmp.lastIndexOf("public");
		}
		if (publicStart != -1) {
			tmp = new String(tmp2.substring(mainStart));
		}
		
		// 得到main方法的结束位置
		char[] tmpChar = tmp.toCharArray();
		int bracketStart = 0, bracketEnd = 0;
		for (int i = 0, len = tmpChar.length; i < len; i++) {
			if (tmpChar[i] == '{') {
				bracketStart++;
			} else if (tmpChar[i] == '}') {
				bracketEnd = i;
				bracketStart--;
			}
			if (i != 0 && bracketStart == 0 && bracketEnd != 0) {
				break;
			}
		}
		StringBuffer contentBuffer = new StringBuffer(5120);
		// 截取字符串
		contentBuffer.append(content.substring(0, publicStart)).append(
				content.substring(mainStart + bracketEnd + 1));
		return contentBuffer.toString();
	}

	public String getFileContent(String fileName, String chartSet)
			throws Exception {
		if (chartSet == null) {
			chartSet = "utf-8";
		}
		StringBuffer buffer = new StringBuffer(5120);
		String line = null;
		InputStream is = new FileInputStream(fileName);
		BufferedReader reader = new BufferedReader(new InputStreamReader(is,
				chartSet));
		while ((line = reader.readLine()) != null) {
			buffer.append(line).append("#_end_#");
		}
		return buffer.toString();
	}

	public void writeStrToFile(String str, String filePath, String charsetName)
			throws Exception {
		if (charsetName == null) {
			charsetName = "utf-8";
		}
		OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(
				filePath), charsetName);
		out.write(str);
		out.close();
	}
}

   测试文件为:

  

public class SimpleTest {
	//方法一
	public void fun() {
		System.out.println("hello world");
	}

	/**
	 * 方法二
	 * @param args
	 */
	public static void mainTest(String[] args) {
		System.out.println("another method");
	}

	//方法三
	public static void Tmain(String[] args) {
		System.out.println("another Method!");
	}

	/**
	 * 方法四
	 */
	public static void main() {
		System.out.println("another Method!");
	}

	/**
	 * 方法五
	 * @param args
	 * @param len
	 */
	public static void main(String[] args, int len) {
		System.out.println("another Method!");
	}

	/**
	 * 方法六
	 * @param args2
	 * @throws Exception
	 */
/*	public static void main(String... args2)throws Exception {
		System.out.println("Main Method!");
		for (int i = 0; i < 10; i++) {
			if (i == 3) {
				System.out.println("----------");
			}
			System.out.println(i);
		}
		System.out.println("another Method!");
	}*/

	/**
	 * 方法七
	 * @param args2
	 * @throws Exception
	 */
	public static void main(String... args2) throws Exception {
		System.out.println("Main Method!");
		String tmp="abcdefbachbad";
		System.out.println(tmp.substring(0,3)+"----="+tmp.substring(3));
		//tmp="/*	 */";
		//tmp="/*\r\n *\r\n **/";
		tmp=tmp.replaceAll("\\/\\*\\s*\\*\\/", "");
		tmp=tmp.replaceAll("/\\*(\\s*\\*\\s*\\s*?)*\\*\\/", "");
		System.out.println(tmp);
		for (int i = 0; i < 10; i++) {
			if (i == 3) {
				System.out.println("----------");
			}
			System.out.println("---------------"+i);
		}
		System.out.println("another Method!");
	}

	/**
	 * 方法八
	 * @param args
	 */
	/*
	 * public static void main(String... args2)throws Exception {
	 * System.out.println("Main Method!"); for (int i = 0; i < 10; i++) { if (i
	 * == 3) { System.out.println("----------"); } System.out.println(i); }
	 * System.out.println("another Method!"); }
	 */

	/**
	 * 方法九
	 */
	public static void main4Test(String... args) {
		System.out.println("another method");
	}

	/**
	 * 方法十
	 */
	public void fun2() {
		System.out.println("hello world2");
	}

	/**
	 * 方法十一
	 */
	public void main5Test() {
		System.out.println("another method");
	}

}

   处理结果为:

  

     测试文件二:

    

public class SimpleTest {
	//方法一
	public void fun() {
		System.out.println("hello world");
	}

	/**
	 * 方法二
	 * @param args
	 */
	public static void mainTest(String[] args) {
		System.out.println("another method");
	}

	//方法三
	public static void Tmain(String[] args) {
		System.out.println("another Method!");
	}

	/**
	 * 方法六
	 * @param args2
	 * @throws Exception
	 */
/*	public static void main(String... args2)throws Exception {
		System.out.println("Main Method!");
		for (int i = 0; i < 10; i++) {
			if (i == 3) {
				System.out.println("----------");
			}
			System.out.println(i);
		}
		System.out.println("another Method!");
	}*/
	
	/**
	 * 方法四
	 */
	public static void main() {
		System.out.println("another Method!");
	}

	/**
	 * 方法五
	 * @param args
	 * @param len
	 */
	public static void main(String[] args, int len) {
		System.out.println("another Method!");
	}

	/**
	 * 方法七
	 * @param args2
	 * @throws Exception
	 */
	public static void main(String... args2) throws Exception {
		System.out.println("Main Method!");
		String tmp="abcdefbachbad";
		System.out.println(tmp.substring(0,3)+"----="+tmp.substring(3));
		//tmp="/*	 */";
		//tmp="/*\r\n *\r\n **/";
		tmp=tmp.replaceAll("\\/\\*\\s*\\*\\/", "");
		tmp=tmp.replaceAll("/\\*(\\s*\\*\\s*\\s*?)*\\*\\/", "");
		System.out.println(tmp);
		for (int i = 0; i < 10; i++) {
			if (i == 3) {
				System.out.println("----------");
			}
			System.out.println("---------------"+i);
		}
		System.out.println("another Method!");
	}


	/**
	 * 方法九
	 */
	public static void main4Test(String... args) {
		System.out.println("another method");
	}

	/**
	 * 方法十
	 */
	public void fun2() {
		System.out.println("hello world2");
	}

	/**
	 * 方法八
	 * @param args
	 */
	/*
	 * public static void main(String... args2)throws Exception {
	 * System.out.println("Main Method!"); for (int i = 0; i < 10; i++) { if (i
	 * == 3) { System.out.println("----------"); } System.out.println(i); }
	 * System.out.println("another Method!"); }
	 */
	
	/**
	 * 方法十一
	 */
	public void main5Test() {
		System.out.println("another method");
	}

}

   处理结果为:

  

     测试文件三:

    

public class SimpleTest2 {
	/**
	 * @param args
	 * @param len
	 */
	public static void main(String[] args, int len) {
		System.out.println("another Method!");
	}
	
	/**
	 * 方法九
	 */
	public static void main4Test(String... args) {
		System.out.println("another method");
	}

	/**
	 * 方法十
	 */
	public void fun2() {
		System.out.println("hello world2");
	}
}

   处理结果为:

  

      由于本人是采用匹配main其实位置截取字符串做的,主要缺点为:

      如果main方法上面有注释,导致main方法去掉后,注释还在,如果main方法上面无注释:

     

public class SimpleTest {
	//方法一
	public void fun() {
		System.out.println("hello world");
	}

	/**
	 * 方法二
	 * @param args
	 */
	public static void mainTest(String[] args) {
		System.out.println("another method");
	}

	//方法三
	public static void Tmain(String[] args) {
		System.out.println("another Method!");
	}

	/**
	 * 方法四
	 */
	public static void main() {
		System.out.println("another Method!");
	}

	/**
	 * 方法五
	 * @param args
	 * @param len
	 */
	public static void main(String[] args, int len) {
		System.out.println("another Method!");
	}

/*	public static void main(String... args2)throws Exception {
		System.out.println("Main Method!");
		for (int i = 0; i < 10; i++) {
			if (i == 3) {
				System.out.println("----------");
			}
			System.out.println(i);
		}
		System.out.println("another Method!");
	}*/

	public static void main(String... args2) throws Exception {
		System.out.println("Main Method!");
		String tmp="abcdefbachbad";
		System.out.println(tmp.substring(0,3)+"----="+tmp.substring(3));
		//tmp="/*	 */";
		//tmp="/*\r\n *\r\n **/";
		tmp=tmp.replaceAll("\\/\\*\\s*\\*\\/", "");
		tmp=tmp.replaceAll("/\\*(\\s*\\*\\s*\\s*?)*\\*\\/", "");
		System.out.println(tmp);
		for (int i = 0; i < 10; i++) {
			if (i == 3) {
				System.out.println("----------");
			}
			System.out.println("---------------"+i);
		}
		System.out.println("another Method!");
	}

	/*
	 * public static void main(String... args2)throws Exception {
	 * System.out.println("Main Method!"); for (int i = 0; i < 10; i++) { if (i
	 * == 3) { System.out.println("----------"); } System.out.println(i); }
	 * System.out.println("another Method!"); }
	 */

	/**
	 * 方法九
	 */
	public static void main4Test(String... args) {
		System.out.println("another method");
	}

	/**
	 * 方法十
	 */
	public void fun2() {
		System.out.println("hello world2");
	}

	/**
	 * 方法十一
	 */
	public void main5Test() {
		System.out.println("another method");
	}

}

   测试结果为:

   

    可见处理结果很正确,残留的注释本人怕误删,所以没做进一步处理。

    如果你有更好的方法,欢迎留言,谢谢。

    本文系原创,转载请注明出处,谢谢。

  

  • 大小: 92.9 KB
  • 大小: 103.5 KB
  • 大小: 58.3 KB
  • 大小: 87.1 KB
0
0
分享到:
评论

相关推荐

    java中遍历某个目录下的所有文件及文件夹中的文件

    1. **`java.io.File` 类**: 这个类是Java中用于处理文件系统的核心类之一。它可以用来创建、删除文件或目录,也可以获取文件的相关属性,如路径、名称等。 2. **`File.listFiles()` 方法**: 此方法返回一个数组,...

    Java开发技术大全(500个源代码).

    HelloNative.java 准备调用C函数的java文件 HelloNative.lib 用VC编译生成的静态库文件 HelloNative.obj 用VB编译生成的目标文件 HelloNativeTest.java 测试本地化是否成功的类文件 instanceVar.java 定义一个...

    mysql-connector-java-8.0.23.jar

    MySQL Connector/J 8.0.23 是MySQL数据库与Java应用程序之间的重要桥梁,它是一个用于连接Java应用程序到MySQL服务器的JDBC驱动程序。这个jar文件是MySQL官方提供的,确保了与MySQL数据库的高效且可靠的通信。在Java...

    java中File类的delete方法详解.doc

    在Java编程语言中,`File`类是处理文件和目录的核心类之一。通过`File`类,开发者能够实现对文件系统的各种操作,比如创建文件、删除文件、读取文件属性等。其中,`delete()`方法是用于删除文件或目录的一个重要方法...

    最新的mysql的jar包,mysql-connector-java-8.0.12

    对于传统的Java应用,这通常意味着将jar文件放入`lib`目录或者在编译和运行命令中指定其位置。对于使用构建工具如Maven或Gradle的现代项目,你可以通过在`pom.xml`或`build.gradle`文件中添加相应的依赖来实现。 ...

    NotesManager-main (1)_java_gestion_

    【标题】"NotesManager-main (1)_java_gestion_" 暗示这是一个使用Java语言开发的笔记管理应用程序。这个项目可能包含一个主程序或者核心模块,用于管理和组织各种笔记。 【描述】"gestion avec java rar" 提到的是...

    java图书管理系统

    模型(Model)通常包含实体类,比如Book类,它可能包含了书籍的基本信息,如书名、作者、出版社等属性,以及与之相关的操作方法,如添加、删除、查询书籍等。这些操作会直接操作数据存储,尽管在这个例子中"无数据库...

    java实现sqlite的db文件增删改查

    这两个版本的驱动都是SQLite JDBC驱动,用于在Java应用程序中连接到SQLite数据库。较新的sqlite-jdbc-3.23.1.jar包含了一些修复和改进,推荐使用这个版本。 添加依赖:在你的项目中,将sqlite-jdbc-3.23.1.jar添加...

    java高级特性 - io流.pdf

    示例代码中首先导入了java.io包下的所有类,并定义了一个hello类和main方法,然后通过File类创建了一个新文件“hello.txt”。这里需要注意的是,在创建文件时使用了File类的静态变量separator和pathSeparator来获取...

    java的mysql驱动

    Java的MySQL驱动,也被称为MySQL Connector/J,是MySQL数据库与Java应用程序之间的重要桥梁。这个驱动程序使得Java开发者能够使用JDBC(Java Database Connectivity)接口来连接、查询和操作MySQL数据库。MySQL ...

    java源码:INI文件操作类库 [ini4j].zip

    在上述代码中,我们首先创建了一个`Ini`对象,然后通过`get()`方法读取指定节和键的值,用`put()`方法写入新的键值对,使用`remove()`方法删除键或节,并最终调用`save()`方法将更改写回文件。 ini4j-0.5.2是这个库...

    简单音乐播放器java语言

    在Player.java文件中,程序的入口点可能是`main`方法。在这个方法中,开发者首先需要加载音频文件,这通常通过`AudioSystem.getAudioInputStream()`完成,传入音频文件的路径作为参数。然后,使用`AudioFormat`获取...

    java jre7里的nio实现稳健监控

    Java JRE7引入了NIO.2(New I/O 2)框架,显著改进了对文件系统操作的支持,其中最重要的增强之一就是引入了WatchService服务。这个服务允许程序以一种高效且优雅的方式监控文件或目录的变化,而无需像JRE6之前那样...

    mysql-connector-java-5.1.35 MySQL的jdbc驱动jar

    MySQL的jdbc驱动jar文件,如“mysql-connector-java-5.1.35”,是Java应用程序连接到MySQL服务器的核心组件。这个驱动程序实现了JDBC接口,使得Java开发者能够通过标准的Java代码执行SQL查询,管理数据库事务,处理...

    日历记事本5(Java源代码)

    在NetBeans中,开发者通常会创建Java项目,然后将源代码组织成包(package),每个包下可能包含对应的类文件,如主类(Main class)、模型类(Model class)、视图类(View class)和控制器类(Controller class)。...

    mysql-connector-java-5.1.46.zip

    2. **SQL语句执行**:它将Java应用程序中的SQL命令转化为MySQL服务器能理解的格式,执行查询、更新、插入和删除等操作。 3. **结果集处理**:返回查询结果给Java应用,可以以ResultSet的形式展示数据。 4. **事务...

    Java-NIO2教程

    ### Java-NIO2教程知识点详解 #### I/O发展简史 - **JDK1.0-1.3**: 在此期间,Java的I/O模型主要依赖于...这只是Java NIO2中众多强大功能之一,通过这些新的API,开发人员可以更高效、简洁地处理文件系统相关的操作。

    Java综合性实验 - 学生考试成绩分析程序

    1. Main.class:这是程序的主入口点,启动应用程序的地方,通常包含`main`方法。 2. ShowFrame.class:可能是用于显示主界面的类,框架可能包含了整个应用的UI元素。 3. ScoreDialog.class:用于处理与成绩相关的...

    mysql-connector-java

    MySQL Connector/J是MySQL数据库系统与Java应用程序之间的桥梁,它是一个实现了Java Database Connectivity (JDBC) API的驱动程序,使得Java开发者能够轻松地在MySQL数据库上执行CRUD(创建、读取、更新、删除)操作...

Global site tag (gtag.js) - Google Analytics