?????工作中常常要写报表sql,每次都想把sql全部小写,但是sql参数不能改变,参数以:开头,写了个简单的类把sql语句转成小写,方便自己使用。
???? 转换前:
?????
?
???? 转换后:
????
??
??? 代码如下,很简单,我就不解释了:
????
class="java" name="code">import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.regex.Matcher; import java.util.regex.Pattern; public class SQL转换为小写 { public static void main(String[] args) { sqlToLowerCase(); } public static void sqlToLowerCase() { String FileName = "src/test.txt"; try { FileReader rFile = new FileReader(FileName); BufferedReader br2 = new BufferedReader(rFile); StringBuffer context = new StringBuffer(); String str = null; Pattern pattern = Pattern.compile(":[a-z_A-Z0-9]*"); Matcher matcher = null; char c = ' '; int tmpIndex = -1; while ((str = br2.readLine()) != null) { while ((tmpIndex = str.indexOf(":")) != -1) { for (int i = 0; i < tmpIndex; i++) { c = str.charAt(i); context.append(Character.toLowerCase(c)); } str = new String(str.substring(tmpIndex)); matcher = pattern.matcher(str); if (matcher.find()) { context.append(matcher.group()); str = new String( str.substring(matcher.group().length())); } } for (int i = 0; i < str.length(); i++) { c = str.charAt(i); context.append(Character.toLowerCase(c)); } context.append("\n"); str = null; } str = null; str = new String(context.toString()); context = null; FileWriter wFile = new FileWriter(FileName); wFile.write(str); System.out.println("修改成功"); rFile.close(); br2.close(); wFile.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
??
????本文系原创,转载请注明出处。谢谢。
???? 全文完。???
??
?