把C语言中的enum转成java里面的整型常量
2012-05-29 11:27
239 查看
public static void enum_c_2_java(String de_filename, String sr_filename) { try { FileReader fr = new FileReader(sr_filename);//创建FileReader对象,用来读取字符流 BufferedReader br = new BufferedReader(fr); //缓冲指定文件的输入 FileWriter fw = new FileWriter(de_filename);//创建FileWriter对象,用来写入字符流 BufferedWriter bw = new BufferedWriter(fw); //将缓冲对文件的输出 String myreadline; //定义一个String类型的变量,用来每次读取一行 boolean bfind = false; int num = -1; boolean isStr = false; String intStr = null; while (br.ready()) { myreadline = removeSpecilChar(br.readLine());//读取一行 if (myreadline.indexOf("enum") > -1) { bfind = true; continue; } if (bfind && myreadline.indexOf("{") > -1) { continue; } if (bfind && myreadline.indexOf("}") > -1) { bw.write("\r\n\r\n"); //写入文件 bfind = false; num = -1; isStr = false; intStr = null; continue; } if (bfind) // 找到enum成员 { int nset = 0; if (myreadline.startsWith("//") || myreadline.length()==0) { bw.write(myreadline); //写入文件 } else { if ((nset = myreadline.indexOf("=")) > -1) { intStr = myreadline.substring(nset + 1, myreadline.indexOf(",")); try { if(intStr.startsWith("0x") || intStr.startsWith("0X")) { intStr = intStr.substring(2); num = Integer.valueOf(intStr,16); intStr = null; } else if(intStr.startsWith("0") && intStr.length() > 1) { intStr = intStr.substring(1); num = Integer.valueOf(intStr,8); intStr = null; }else if((intStr.charAt(0)>='A' &&(intStr.charAt(0)<='Z') || (intStr.charAt(0)>='a'&&intStr.charAt(0)<='z'))){ isStr = true; }else { num = Integer.valueOf(intStr); intStr = null; } bw.write(get_str(myreadline.substring(0, nset), intStr, num)); if (isStr) num = 0; /* bw.write(myreadline.substring(0, nset)); bw.write(" = "+num + ";"); //写入文件 */ } catch (NumberFormatException e) { // TODO Auto-generated catch block e.printStackTrace(); /* isStr = true; num = -1; bw.write(get_str(myreadline.substring(0, myreadline.length() - 2), intStr, num)); num = 0; */ /* bw.write(myreadline.substring(0, myreadline.length() - 2)); //写入文件 bw.write(";"); */ } } else if ( myreadline.indexOf(",") > -1) { ++num; /* bw.write(myreadline.substring(0, myreadline.length() - 1)); //写入文件 if(isStr) bw.write(" = " + (intStr + "+" + num) + ";"); else bw.write(" = "+num + ";"); */ bw.write(get_str(myreadline.substring(0, myreadline.length() - 1), intStr, num)); } else { /* bw.write(myreadline); //写入文件 if(isStr) bw.write(" = " + (intStr + "+" + num) + ";"); else bw.write(" = "+num + ";"); */ ++num; bw.write(get_str(myreadline, intStr, num)); } } bw.newLine(); bw.flush(); //System.out.println(myreadline);//在屏幕上输出 } } bw.flush(); //刷新该流的缓冲 bw.close(); br.close(); fw.close(); br.close(); fr.close(); } catch (IOException e) { e.printStackTrace(); } } public static String get_str(String str, String instr, int num) { if (str == null) return null; if (instr != null) str = str+" = "+instr; if (num >= 0 && instr != null) str = str + " + " + num; else if(num >= 0) str = str + " = " + num; str = str + ";"; System.out.println(str);//在屏幕上输出 return "public final static int "+str; } public static String removeSpecilChar(String str){ String result = ""; if(null != str){ Pattern pat = Pattern.compile("\\s*|\n|\r|\t"); Matcher mat = pat.matcher(str); result = mat.replaceAll(""); } return result; }
相关文章推荐
- Effective Java(优先考虑类型安全的异构容器、用enum代替int常量)
- 用c语言 简单实现JAVA里面的ArryList 功能,实现内存自动扩展
- Java 常量定义-枚举enum
- C语言的整型常量与实型常量
- java里面整型数组元素进行冒泡排序
- c语言中的整型常量和实型常量
- java基础总结 --- enum枚举的常量相关方法、自动售货机(状态模式)例子
- c语言与java语言判断一个点在一个多边形里面
- c语言中一些整型常量的范围
- Effective Java(优先考虑类型安全的异构容器、用enum代替int常量)
- Java枚举(ENUM)用法之常量
- 使用javap深入理解Java整型常量和整型变量的区别
- 枚举(enum)类型是Java 5新增的特性,它是一种新的类型,允许用常量来表示特定的数据片断,而且全部都以类型安全的形式来表示。
- Effective Java(优先考虑类型安全的异构容器、用enum代替int常量)
- Effective Java之用enum代替int常量(三十)
- Effective Java(优先考虑类型安全的异构容器、用enum代替int常量)
- Java实现读数字的问题,输入长整型数字,输出汉语里面的读法
- Effective Java(优先考虑类型安全的异构容器、用enum代替int常量)
- java enum学习和用法,代替静态常量
- C语言整型常量的表示方法(转自百度整理)