mysql 去多余回车

来源:互联网 发布:淘宝装修神器 编辑:程序博客网 时间:2024/05/21 11:00
package mytools;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Practice7 {
    public static void main(String[] args) {
        try {
            System.gc();
            FileInputStream fis = new FileInputStream("F:\\temp\\c.sql");
            InputStreamReader isr = new InputStreamReader(fis, "UTF-8");
            BufferedReader br = new BufferedReader(isr);

            FileOutputStream fos = new FileOutputStream("F:\\temp\\bb.sql");
            OutputStreamWriter osw = new OutputStreamWriter(fos, "UTF-8");
            BufferedWriter bw = new BufferedWriter(osw);
            int a = 0;
            String strLine;
            StringBuffer sb = new StringBuffer();
            
            while ((strLine = br.readLine()) != null) {
                
                if (strLine.startsWith("INSERT INTO")) {
                    /*针对添加数据的行进行格式化*/
                    //判断此行 是否为完整的字段
                    if (strLine.endsWith("),") || strLine.endsWith(");")
                            || strLine.endsWith("*/")
                            || strLine.endsWith("*/;")
                            || strLine.endsWith("--")) {
                        sb.append(strLine);
                        strLine = sb.toString();
                        sb.delete(0, sb.length()); // 清空sb
                        // 格式化操作 其他不用格式化
                        // 天幻换行符
                        strLine = getStringNoBlank(strLine);
                        // strLine = GSH(strLine); // 格式化
                        // strLine = InsertInto(strLine); // insert
                        //strLine = dofen(strLine); // 去掉回车换行
                        
                        bw.write(strLine + "\n");
                        
                    } else {
                        sb.append(strLine);
                        System.out.println(strLine+"出发现异常");
                        
                    }
                    System.out.print(a + "\t");
                    a++;
                }else{
                    //非添加数据的行,读一行 写一行
                    bw.write(strLine + "\n");
                    System.out.print(a + "\t");
                    a++;
                }
                //输出加换行
                
                if (a % 10 == 0) {
                    System.out.println();
                }
            }
            //正确的导出SQL不会执行到.
            if (sb.length() > 0) {
                System.out.println("SQL备份文件有错,请重新导入");
            }
            System.gc();
            br.close();
            bw.close();
            fos.close();
            fis.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println("Error: " + e.getMessage());
        }
    }

    // 替换回车
    public static String getStringNoBlank(String str) {
        if (str != null && !"".equals(str)) {
            Pattern p = Pattern.compile("\\n");
            Matcher m = p.matcher(str);
            String strNoBlank = m.replaceAll("");
            return strNoBlank;
        } else {
            return str;
        }
    }

    // 格式化 [\\)],[\\s]{0,n}[\\(]
    public static String GSH(String str) {
        if (str != null && !"".equals(str)) {
            Pattern p = Pattern.compile("\\)\\s*\\,\\s*\\(");
            Matcher m = p.matcher(str);
            String strNoBlank = m.replaceAll("\\)\\,\n\t\\(");
            return strNoBlank;
        } else {
            return str;
        }
    }

    // 格式化 [\\)],[\\s]{0,n}[\\(]
    public static String InsertInto(String str) {
        if (str != null && !"".equals(str)) {
            Pattern p = Pattern.compile("VALUES");
            Matcher m = p.matcher(str);
            String strNoBlank = m.replaceAll("VALUES\n\t");
            return strNoBlank;
        } else {
            return str;
        }
    }
}
0 0
原创粉丝点击