java實現編輯器(一)

GeekWay發表於2011-11-19

 昨天下半天的作品,不敢獨享,發程式碼以拋磚引玉,望高手指點!!!


初步完成了四項基本功能:
1,提取檔案。
2,刪除註釋。
3,關鍵字識別。
4,符號匹配。


原始碼:

import java.io.File;				//用於建立檔案
import java.io.IOException ;		//用來處理異常資訊
import java.io.InputStream ;
import java.io.FileInputStream ;
public class Compiler {
	//功能1. 將註釋刪除掉 這裡先將“//”的全部註釋刪除,暫沒考慮“/*....*/”和“/*....**/”的情況
	public static void deleteAnnotation(String s){
		StringBuffer Sb=new StringBuffer(s);
		int annotationLocation=0;
		int lineFeedLocation=0;
//		
//		if(Sb.lastIndexOf("//")>Sb.lastIndexOf("\n")){		//消除最後一行沒有回車的註釋,不然影響後面程式的執行
//			annotationLocation=Sb.lastIndexOf("//");
//			Sb.delete(annotationLocation, Sb.length());
//			}
		while(-1!=Sb.indexOf("//")){
			annotationLocation=Sb.indexOf("//",0);						//記錄註釋“//”出現的位置
				lineFeedLocation=Sb.indexOf("\n",annotationLocation);		//記錄離“//”最近的回車換行的位置	
			Sb.delete(annotationLocation, lineFeedLocation);				//將註釋刪除
		}

		System.out.println(Sb.toString());	//驗證結果
	}


	// 功能2.  下面的方法完成找到指定關鍵字,並統計字串中某關鍵字(key)的個數
	public static void keyCount(String s,String key){	
		short[] location=new short[256];	//記錄key的位置
		StringBuffer Sb= new StringBuffer(s);
		int keyCount=0;				//暫存關鍵字的個數	
		int temp=0;					//暫存int位置的資訊
		int locationBuffer=0;		//
		while(-1!=Sb.indexOf(key)){
			//此條件用來避免關鍵字(key)在含在普通字元中的情況,如int出現在“print”中。
			if(Sb.charAt(Sb.indexOf(key)-1)==' '&&Sb.charAt(Sb.indexOf(key)+key.length())==' '||Sb.charAt(Sb.indexOf(key)+key.length())=='['){	//此處還有bug!
				keyCount++;	
				location[locationBuffer]=(short)Sb.indexOf(key);
				locationBuffer++;
			}
			temp=Sb.indexOf(key);	
			Sb.delete(0,temp+key.length());		//將含有key前的所有字元均刪除			
		}
		if(keyCount>0){
			System.out.println("關鍵字"+key+"在檔案中有:"+ keyCount+"個。");
			System.out.print(key+"在該檔案的所在位置(單位:字元):");
			for(int i=0;i<locationBuffer;i++){
				System.out.print(location[i]+"\t");	
			}
			System.out.println('\n');
		}
	}
	//功能3. 判斷需要“互相匹配(matching)”的字元,如“大括號”、“小括號”等
	public static void matching(String s,char characterLeft,char characterRight){
		int CharacterCount=0;
		char c=' ';
		for(int i=0;i<s.length();i++){		//計數
			if(characterLeft==s.charAt(i)){CharacterCount++;	}
			if(characterRight==s.charAt(i)){CharacterCount--;	}
		}
		//輸出驗證
		if(CharacterCount!=0){
			System.out.print("(⊙_⊙!),編譯出錯: \" "+characterLeft+" \" 和"+"\" "+characterRight+" \" 不配對,");
			if(CharacterCount>0)
				System.out.println("少了"+CharacterCount+"個\" "+characterRight+" \"");
			else
				System.out.println("多出"+(-CharacterCount)+"個\" "+characterRight+" \"");
		}
	}
	public static void main(String args[])throws Exception{	//此處為了程式碼簡潔,直接將異常交給JVM(java虛擬機器)了
		//功能3 讀入檔案
		// 使用File類找到一個檔案
		File f=new File("e:\\Hello.java");		//★★此處你可以在你的電腦上找一個檔案(如*.java)進行驗證,注:此處有2個反斜槓“\”。★★
		//讀入E盤的“Hello.java”檔案				
		// 通過子類例項化父類物件
		InputStream input = null ;	// 準備好一個輸入的物件
		input = new FileInputStream(f)  ;	// 通過物件多型性,向上轉型進行例項化(⊙o⊙)哦
		byte b[] = new byte[1024] ;		// 所有的內容都讀到此陣列之中
		input.read(b) ;		// 讀取內容
		input.close() ;						// 關閉輸出流
		
		String ss=new String(b ) ;		 //把byte陣列變為字串輸出
		String s=" "+ss+"\n";			//加" "為了字元位置的匹配,
										//加"\n"為了消除註釋在最後時沒有回車符的情況
		Compiler.deleteAnnotation(s);
		Compiler.keyCount(s,"main");		//此處有bug!待解
		Compiler.keyCount(s,"class");
		Compiler.keyCount(s,"public");		//此處有bug!待解
		Compiler.keyCount(s,"static");

		Compiler.keyCount(s,"void");
		Compiler.keyCount(s,"int");
		Compiler.keyCount(s,"double");
		Compiler.keyCount(s,"float");
		Compiler.keyCount(s,"char");
		Compiler.matching(s, '{', '}');
		Compiler.matching(s, '[', ']');
		Compiler.matching(s, '(', ')');
	}
}
//執行結果:
//關鍵字class在檔案中有:1個。
//class在該檔案的所在位置(單位:字元):8	

//關鍵字public在檔案中有:2個。
//public在該檔案的所在位置(單位:字元):1	21	

//關鍵字static在檔案中有:1個。
//static在該檔案的所在位置(單位:字元):35	

//關鍵字void在檔案中有:1個。
//void在該檔案的所在位置(單位:字元):42	

//關鍵字int在檔案中有:3個。
//int在該檔案的所在位置(單位:字元):23	15	66	

//關鍵字double在檔案中有:1個。
//double在該檔案的所在位置(單位:字元):158	

//關鍵字float在檔案中有:1個。
//float在該檔案的所在位置(單位:字元):186	

//(⊙_⊙!),編譯出錯: " { " 和" } " 不配對,多出5個" } "



相關文章