转换sql文件的create table语句为drop table语句

时间:2022-02-09 15:22:58
 1 package com.csii.pweb.query.action;
 2 
 3 import java.io.BufferedReader;
 4 import java.io.FileNotFoundException;
 5 import java.io.FileReader;
 6 import java.io.IOException;
 7 import java.util.ArrayList;
 8 import java.util.List;
 9 
10 
11 public class JavaFile {
12     public static void main(String[] args) {
13         try {
14             // read file content from file
15             StringBuffer sb= new StringBuffer("");
16 
17             FileReader reader = new FileReader("D:/t/workflow.sql");
18             BufferedReader br = new BufferedReader(reader);
19 
20             String str = null;
21             final String markStart = "CREATE TABLE";
22             List<String> tableNames = new ArrayList<String>();
23             while((str = br.readLine()) != null) {
24                 str = str.toUpperCase().trim(); //转为大写并去除两端空格
25                 if(str.startsWith(markStart) && !str.startsWith("--")) {    //非注释之建表语句
26                     str = str.substring(markStart.length()).replace('(', ' ');    //取表名
27                     tableNames.add("DROP TABLE " + str.trim()+";");
28 //                    System.out.println(str);
29                 }
30             }
31 
32             br.close();
33             reader.close();
34             //倒序
35             for(int i=tableNames.size()-1; i>=0;i--) {
36                 String name = tableNames.get(i);
37                 sb.append(name).append("\n");
38             }
39             System.out.println("--------------start---------------");
40             System.out.println(sb);
41             System.out.println("--------------end---------------"+tableNames.size());
42 
43             // write string to file
44 //            FileWriter writer = new FileWriter("c://test2.txt");
45 //            BufferedWriter bw = new BufferedWriter(writer);
46 //            bw.write(sb.toString());
47 //
48 //            bw.close();
49 //            writer.close();
50         }
51         catch(FileNotFoundException e) {
52             e.printStackTrace();
53         }
54         catch(IOException e) {
55             e.printStackTrace();
56         }
57     }
58 }