读取资料中的字符然后拼接成SQL再写入到文件
读取文件中的字符然后拼接成SQL再写入到文件
public static void main(String[] args) { File file = new File("E:/test.txt"); BufferedReader buffer = null; File into = new File("E:/test1.txt"); int i = 0; String line = null; InputStream stream = null; OutputStream out = null; String charset = "gb2312";//UTF-8 tkf_sgip_receive String sql1 = "insert into TCZ_MMSWN_RECEIVE(OID ,USERNUMBER, CONTENT, RTIME, NEXTTIME, ADDTIME,MONEY, " + "STATUS, CHANGEAGENT, AREA_NO) " + "values ('";//03-10-2009 09:00:00 String sql2 = "', 'hf', to_date('";//8613187781793 String sql3 = "', 'yyyy-mm-dd hh24:mi:ss'), sysdate+0.1, 7, 200, 11, 'dzjl_"; String sql4 = "', '0871');"; String resultSQL = ""; // String sql1 = "insert into tkf_sgip_receive(ID,USERNUMBER, CONTENT, RTIME, NEXTTIME, ADDTIME,MONEY, " + // "STATUS, CHANGEAGENT, AREA_NO) " + // "values ('";//03-10-2009 09:00:00 // String sql2 = "', 'hf', to_date('";//8613187781793 // String sql3 = "', 'yyyy-mm-dd hh24:mi:ss'), sysdate+0.1, 7, 200, 11, 'dzjl_"; // String sql4 = "', '0871');"; // String resultSQL = ""; try { stream = new FileInputStream(file); byte [] by = new byte[1024]; out = new FileOutputStream(into); buffer = new BufferedReader(new InputStreamReader(stream , charset)); while((line = buffer.readLine()) != null){ // if(i == 2){ // break; // } int index = line.indexOf("费"); String num = line.substring(index + 2,index + 13); System.out.println("str =" + num); index = line.indexOf("dzjl"); String no = line.substring(index + 5 , index + 10); System.out.println("no =" + no); String de = ""; if((index + 27) > line.length()){ de = line.substring(index + 11 , index + 26); }else{ de = line.substring(index + 11 , index + 27); } System.out.println("de =" + de); resultSQL = sql1 + num + "','" + num + sql2 + de + ":00" + sql3 + no + sql4 + "\r\n"; if(i == 100){ resultSQL = resultSQL + "commit;" + "\r\n"; i = 0; } //System.out.println(resultSQL); out.write(resultSQL.getBytes()); i++; System.out.println(" i = " + i); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { try { if(null != stream){ stream.close(); } if(null != out){ out.close(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }