自写格式化sql文件


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class modifyFileContext {

	public static void main(String[] args) throws IOException {
		String baseReadPath = "C:\\Users\\Administrator\\Desktop\\源文件\\";
		String baseWritePath = "C:\\Users\\Administrator\\Desktop\\修改后的文件\\";
//		String fileName="3.06更新CR_GroupCustomerEndList报表.sql";
//		String fileName="3.07更新CR_MasterSlaveSummary报表.sql";
//		String fileName="3.08更新CR_PaymentDetailReportForLD报表.sql";
		String fileName="5.02更新娄底市收费查询报表的sqltext字段.tst";
//		String fileName="3.10更新CR_SubAreaPaymentsForHY报表.sql";
//		String fileName="3.11更新CR_WirelessCompanyTVUser报表.sql";
		String inFilePath = baseReadPath + fileName;
		String outFilePath = baseWritePath + fileName;
		BufferedReader br = null;
		BufferedWriter bw = null;
		try {
//			br = new BufferedReader(new FileReader(inFilePath));
		    br = new BufferedReader(new InputStreamReader(new FileInputStream(inFilePath),"GBK"));
//			bw = new BufferedWriter(new FileWriter(outFilePath));
			bw = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(outFilePath), "GBK"));
			String str = null;
			int i=1;
			while ((str = br.readLine()) != null) {
				bw.write(rTrim(str) + "\r\n");
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			bw.flush();
			br.close();
			bw.close();
			System.out.println("完成");

		}

	}

	@SuppressWarnings("deprecation")
	public static  synchronized String rTrim(String str) {
	    int len=0;
        len = str.length();
        if (len == 0) {
            return "";
        }
		char s = str.charAt(len - 1);
		while (Character.isSpace(str.charAt(len - 1))) {
			len--;
			if (len==0) {
			    return "";
            }
		}
		str=str.substring(0, len);
		str=str.replace("nvl(", "NVL(");
		str=str.replace("to_char(", "TO_CHAR(");
		str=str.replace(" update ", " UPDATE ");
		str=str.replace(" in ", " IN ");
		str=str.replace(" trunc(", " TRUNC(");
		str=str.replace(" select ", " SELECT ");
		str=str.replace(" join ", " JOIN ");
		str=str.replace(" order by ", " ORDER BY ");
		str=str.replace(" and ", " AND ");
		str=str.replace(" where ", " WHERE ");
		str=str.replace(" or ", " OR ");
		str=str.replace(" on ", " ON ");
		str=str.replace(" is null", " IS NULL");
		str=str.replace(" end ", " END ");
		str=str.replace("case when ", "CASE WHEN ");
        str=str.replace(" as ", " AS ");
		str=str.replace("union all", "UNION ALL");
		str=str.replace("(sysdate-", "(SYSDATE-");
		str=str.replace(" set ", " SET ");
		str=str.replace("sum(", "SUM(");
		str=str.replace("distinct(", "DISTINCT(");
		str=str.replace("left ", "LEFT ");
		str=str.replace("count(", "COUNT(");
		str=str.replace(" then ", " THEN ");
		str=str.replace(" not ", " NOT ");
	    str=str.replace("nvl(", "NVL(");
	    str=str.replace("join ", "JOIN ");
	    str=str.replace(" in", " IN");
	    str=str.replace("and ", "AND ");
        str = str.replace("order by ", "ORDER BY ");
        str = str.replace(" like ", " LIKE ");
        str = str.replace("null", "NULL");
        str = str.replace("from ", "FROM ");
        str = str.replace("select ", "SELECT ");
        str = str.replace("to_char( ", "TO_CHAR( ");
        str = str.replace("(distinct ", "(DISTINCT");
        str = str.replace(" is ", " IS ");
        str = str.replace(" else ", " ELSE ");
        str=str.replace(" as ", " AS ");
        str=str.replace("insert  ", "INSERT ");
        str=str.replace(" INto ", " INTO  ");
        str=str.replace(" values ", " VALUES ");
        str=str.replace(" group ", " GROUP ");
        str=str.replace(" by ", " by ");
        str=str.replace(" distinct", " DISTINCT ");
        str=str.replace("values (", "VALUES (");
        str=str.replace("insert ", "INSERT ");
        str=str.replace("group by ", "GROUP BY ");
        str=str.replace("where ", "WHERE ");
        return str;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值