奋斗到了凌晨两点半终于把最近项目需要的一个需求搞定了因为客户端与数据库之间的一个数据服务框架命名规则不符合java的命名规则所以这里需要做一个转换大概功能实现了算法方面还有待优化 package readandwrite; import javaioBufferedReader; import javaioBufferedWriter; import javaioFile; import javaioFileInputStream; import javaioFileWriter; import javaioIOException; import javaioInputStreamReader; import javautilArrayList; import javautilregexPattern; public class ReadAndWrite { private static BufferedReader br; private static BufferedWriter bw; /** * @param args * @throws IOException * * 两个问题 javabean非规范命名 如开头大写 时间 Timestamp将导致的插入问题!!!!!! */ public static void main(String[] args) throws IOException { File dir = new File(d:\\test\\hibernate\\) String newPath = d:\\test\\hibernate_new\\; File file = new File(newPath) if(!fileexists()) { filemkdir()//创建文件夹 } File file[] = dirlistFiles() for (int i = ; i < filelength; i++) { if(file[i]isDirectory()) {//文件夹直接跳过 continue; } String filename = file[i]getName() br = new BufferedReader(new InputStreamReader(new FileInputStream(file[i]getAbsolutePath()))) File f = new File(newPath+filename) if(fexists()){ fdelete()//删除旧文件避免重复写入 } fcreateNewFile() FileWriter fw = new FileWriter(ftrue) bw = new BufferedWriter(fw) if(javaequals(exn(filename))) {//解析实体类 String data = ; while((data = ())!=null) { bwwrite(split(data)+\n) bwflush() } } else if(xmlequals(exn(filename))) {//解析映射文件 String data = ; while((data = ())!=null) { bwwrite(sp(data)+\n) bwflush() } } } } /* * 获取文件扩展名 */ public static String exn(String filename) { int k = filenamelastIndexOf() if(k> && k<(filenamelength())) { return filenamesubstring(k+) } else { return ; } } /*==============================================解析java实体类======================================================*/ /* * 拆分关键词变小写 * 规则将带有驼峰格式的子串变小写 */ public static String split(String s) { String data ; String reg = \\s|\\|\\|\\;|\\(|\\)|\\{|\\}|\\\; String[] word = ssplit(reg)//java标识符 ArrayList<String> tag = new ArrayList<String>() String[] tem = ssplit() // 保存标点符号 for(int i=; i<temlength; i++) { if(tem[i]matches(reg)) { tagadd(tem[i]) } } int k = ; for (int i = ; i < wordlength; i++) { String fw = format(word[i]) if(i < tagsize()) { data += fw+tagget(i) } else { data += fw; } k = i; } if(tagsize() > k) { for (int i = k+; i < tagsize() i++) { data += tagget(i) } } return data; } /* * 驼峰格式子串去大写 */ public static String format(String str) { String reg = ([az]+[AZ][az]+)*; Pattern p = pile(reg) if(pmatcher(str)matches()) { return strtoLowerCase() } return str; } /*==============================================解析hbmxml映射文件======================================================*/ /* * 拆分关键词变小写 * 将 组合 id name = || property name = 双引号中的属性值变小写 */ public static String sp(String str) { if(ntains(<id name) || ntains(<property name)) { return split(str) } return str; } } |