赞
踩
通常在项目中,有着许多书写在.sql文件中的sql语句需要进一步加工处理并最终执行完成,那么脚本文件中这些sql语句又应该怎么去读取到呢?
//完整文件路径 String path="C:\test.sql"; //读文件获取sql public static String[] readFile(String path) { InputStream in = null; BufferedReader bufferedReader = null; StringBuffer stringBuffer = new StringBuffer(); try { in = new FileInputStream(path); bufferedReader = new BufferedReader(new InputStreamReader(in,Charset.forName("utf-8"))); String line = null; while ((line = bufferedReader.readLine()) != null) { String s = line; //去掉行注释 if (s.trim().startsWith("--")) { continue; } stringBuffer.append(" ").append(line); } String[] sqls = stringBuffer.toString().split(";"); return sqls; } catch (IOException e) { e.printStackTrace(); } finally { stringBuffer = null; try { if(bufferedReader!=null){ bufferedReader.close(); } if(in != null){ in.close(); } } catch (IOException e) { e.printStackTrace(); } } return null; }
至此,将.sql文件中的全部sql语句都读取到了,后续可以进行相关的加工处理及执行了。
//目标输出
FileWriter writer = new FileWriter("C:\\test.sql");
//众多sql则是在for循环中write
writer.write(sql + "\n");
//成功将sql内容输出到test.sql文件中
writer.close();
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。