首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

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

2014-02-22 
Java应用之删除Java文件Main方法改进???? 上午写的那篇去除Main方法由于本人考虑欠缺,以为一个Java类里面

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

???? 上午写的那篇去除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");}}

?? 处理结果为:

??
Java应用之剔除Java文件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!");}/** * 方法六 * @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");}}

?? 处理结果为:

??
Java应用之剔除Java文件Main方法改进
???? 测试文件三:

????

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");}}

?? 处理结果为:

??
Java应用之剔除Java文件Main方法改进
????? 由于本人是采用匹配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");}}

?? 测试结果为:

???
Java应用之剔除Java文件Main方法改进
??? 可见处理结果很正确,残留的注释本人怕误删,所以没做进一步处理。

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

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

??

热点排行