java实现编辑器(一)

来源:互联网 发布:调色软件app 编辑:程序博客网 时间:2024/06/16 13:17

 昨天下半天的作品,不敢独享,发代码以抛砖引玉,望高手指点!!!


初步完成了四项基本功能:
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+" \"");elseSystem.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在该文件的所在位置(单位:字符):121//关键字static在文件中有:1个。//static在该文件的所在位置(单位:字符):35//关键字void在文件中有:1个。//void在该文件的所在位置(单位:字符):42//关键字int在文件中有:3个。//int在该文件的所在位置(单位:字符):231566//关键字double在文件中有:1个。//double在该文件的所在位置(单位:字符):158//关键字float在文件中有:1个。//float在该文件的所在位置(单位:字符):186//(⊙_⊙!),编译出错: " { " 和" } " 不配对,多出5个" } "