转载于 https://blog.csdn.net/qionglong_jiao/article/details/117298689
在转载的基础上增加了一点点内容,主要是去掉
SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;
SET FOREIGN_KEY_CHECKS = 1;
@Test
public void h2(){
try {
System.out.println(convert("/Users/wesfer/Desktop/app_center.sql"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static String convert(String filePath) throws IOException {
String[] rawSQL = new String(Files.readAllBytes(Paths.get(filePath))).split("\\n");
StringBuilder builder = new StringBuilder();
for(String line : rawSQL) {
if(line.contains("CHARACTER SET utf8 COLLATE utf8_general_ci")) {
line = line.replaceAll("CHARACTER SET utf8 COLLATE utf8_general_ci", "");
} else if(line.contains("INDEX")) {
continue;
} else if(line.contains("IF NOT EXISTS")) {
line = line.replaceAll("IF NOT EXISTS", "");
}else if(line.contains("SET FOREIGN_KEY_CHECKS = 0;")) {
line = line.replaceAll("SET FOREIGN_KEY_CHECKS = 0;", "");
}else if(line.contains("SET NAMES utf8mb4;")) {
line = line.replaceAll("SET NAMES utf8mb4;", "");
}else if(line.contains("SET FOREIGN_KEY_CHECKS = 1;")) {
line = line.replaceAll("SET FOREIGN_KEY_CHECKS = 1;", "");
} else if(line.contains("--")) {
continue;
} else if(line.contains("ENGINE")) {
line = line.replaceAll("\\).*ENGINE.*(?=)", ");");
}else if (line.contains("USING BTREE")){
line = line.replaceAll("USING BTREE*","");
}
line = line.replace("`", "");
builder.append(line).append("\n");
}
return builder.toString();
}