1.读取文件
2.去除注释
3.获取文件中的输入表名(在(二)中)
3.输出到指定目录文件中
sql中的注释有两种情况:
--、/**/
注:本文的文件读写参考其他文章
/**
* 1.读取文件
* 2.删除sql代码中的注释
* 3.将获取到的表名写入文件中
*/
public static void clearComment(File file, String charset) {
try {
//递归处理文件夹
if (!file.exists()) {
System.out.println("null");
return;
}
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File f : files) {
clearComment(f, charset); //递归调用
}
return;
} else if (!file.getName().endsWith(".lob")) {
//指定文件名后缀,非.lob文件直接返回
return;
}
System.out.println("start" + file.getAbsolutePath());
//根据对应的编码格式读取
BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), charset));
StringBuffer content = new StringBuffer();
String tmp = null;
while ((tmp = reader.readLine()) != null) {
content.append(tmp);
content.append("\n");
}
String target = content.toString();
Pattern p = Pattern.compile("(?ms)('(?:''|[^'])*')|--.*?$|///*.*?//*/|\\/\\/[^\\n]*|\\/\\*([^\\*^\\/]*|[\\*^\\/*]*|[^\\**\\/]*)*\\*+\\/");
String presult = p.matcher(target).replaceAll("$1");
//使用对应的编码格式输出
List<String> list=new ArrayList<String>();
list=get_in_table(presult);
String[] result=list.toArray(new String[list.size()]);
//遍历数组并存入集合,如果元素已存在则不会重复存入
for (int i = 0; i < r.length; i++) {
set.add(r[i]);
}
//返回Set集合的数组形式
String[] t=(String[]) set.toArray(new String[set.size()]);
String filename="D://test//"+file.getName().substring(0,file.getName().indexOf(".")+1)+
file.getName().substring(file.getName().indexOf(".")+1).substring(0,file.getName().substring(file.getName().indexOf(".")+1).indexOf("."));
File newFile=new File(filename);
if(!newFile.exists()){
newFile.createNewFile();
}
out=new BufferedOutputStream(new FileOutputStream(newFile));
//这里也可以给定编码写入新文件
ow=new OutputStreamWriter(out,"gb2312");
bw=new BufferedWriter(ow);
//遍历数组把字符串写入新文件中
StringBuffer sb = new StringBuffer();
if(r.length>0){
for(int i = 0; i < t.length-1; i++){
sb. append(t[i]+",");
}
sb.append(t[t.length-1]);
String s = sb.toString();
bw.write(s);
//刷新该流的缓冲,这样才真正写入完整到新文件中
}
bw.flush();
count++;
System.out.println("over" + count);
bw.close();
ow.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}