数据库
首页 > 数据库> > 从一个大的有很多建表语句的文件,拆成一个个小的sql文件

从一个大的有很多建表语句的文件,拆成一个个小的sql文件

作者:互联网

很多时候,很多建表文档都是很多建表语句放在一起,自建手动CV,太麻烦
写个小脚本快速执行

public class Test {
    public static void main(String[] args) throws IOException {
        File file= new File("fost.sql");


        FileReader fileReader = new FileReader(file);

        BufferedReader bufferedReader = new BufferedReader(fileReader);


        String line = bufferedReader.readLine();
        String head = "CREATE TABLE ";
        int a = 0;
        anchor:  // 声明锚点
        while (true){
            System.out.println("=======");

            if(line == null){
                break anchor;
            }
            if (line.contains(head)){

                a++;
                System.out.println(a);
                File file2= new File(line.replace(head, "")+".sql");

                FileWriter fileWriter2 = new FileWriter(file2);
                BufferedWriter bufferedWriter2 = new BufferedWriter(fileWriter2);
                bufferedWriter2.write(line);
                bufferedWriter2.newLine();
                while (true){

                    line = bufferedReader.readLine();
                    if(line.contains(";")){
                        bufferedWriter2.write(line);
                        bufferedWriter2.newLine();
                        bufferedWriter2.close();
                        System.out.println("该开始下一次了");
                        break ;
                    }else if(null == line){
                        bufferedWriter2.close();
                        break anchor;
                    }else {
                        bufferedWriter2.write(line);
                        bufferedWriter2.newLine();
                    }

                }
            }else {
                line = bufferedReader.readLine();
                continue ;
            }
        }

        bufferedReader.close();


    }
}

不知道有没有逻辑问题,随手写的,没太深究,如果不只是想要拆建表,还有insert表的时候,可以设置个变量,去进行判断,改改就行!

标签:拆成,文件,建表,bufferedWriter2,bufferedReader,File,new,line
来源: https://blog.csdn.net/weixin_45931215/article/details/119358868