标签:
import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileReader; import java.io.FileWriter; import java.io.IOException; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ConvertToString { public static Pattern sqlPatern = null; static { String regex = "\\$P\\{(\\w+|\\_)\\}"; sqlPatern = Pattern.compile(regex); } public static void main(String args[]) { convertSQLToJavaString(); } private static void convertSQLToJavaString() { String readPath = "C:\\Users\\Administrator\\Desktop\\sql.txt"; String writePath = "C:\\Users\\Administrator\\Desktop\\out.txt"; File readFile = new File(readPath); File writeFile = new File(writePath); try { writeFile.createNewFile(); } catch (IOException e2) { e2.printStackTrace(); } BufferedReader reader = null; BufferedWriter writer = null; try { reader = new BufferedReader(new FileReader(readFile)); writer = new BufferedWriter(new FileWriter(writeFile)); String line = null; while ((line = reader.readLine()) != null) { line = "\" " + line +" \","; writer.write(replaceParameters(line)); writer.newLine(); } System.out.print("success"); } catch (Exception e) { e.printStackTrace(); try { reader.close(); writer.close(); } catch (IOException e1) { e1.printStackTrace(); } } finally { try { writer.flush(); reader.close(); writer.close(); } catch (IOException e1) { e1.printStackTrace(); } } } private static String replaceParameters(String line) { Matcher m = sqlPatern.matcher(line); StringBuffer sb = new StringBuffer(); while(m.find()) { String parameter = m.group(1); m.appendReplacement(sb, "\‘\" + " + parameter + " + \"\‘"); } m.appendTail(sb); return sb.toString(); } }
标签:
原文地址:http://my.oschina.net/u/1583086/blog/505066