从sql文件中读取sql脚本并使用 java读取sql脚本中的输入表(一)--循环读取多个文件并去除sql文件中的注释

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();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值