java转换unicode,筛选文件中的insert语句并把日期给转换为可以直接在数据库执行的语句

  1 package com;
  2 import java.io.BufferedReader;
  3 import java.io.BufferedWriter;
  4 import java.io.File;
  5 import java.io.FileInputStream;
  6 import java.io.FileWriter;
  7 import java.io.IOException;
  8 import java.io.InputStreamReader;
  9 
 10 import javax.swing.JFileChooser;
 11 import javax.swing.JOptionPane;
 12 
 13 
 14 public class supZhtool_1 {
 15 
 16     public static void main(String[] args) throws IOException {
 17 
 18         String path ="";//源文件路径,不包括文件名称
 19         String pathzh = "";//转换unicode编码后文件输出路径
 20         String pathinsert = "";//筛选出insert语句后的文件输出路径
 21         String pathSource = "";//源文件路径,包含文件名称的源文件路径
 22         
 23         JFileChooser fDialog = new JFileChooser();
 24 
 25         fDialog.setDialogTitle("请选择需要转换的文件");
 26 
 27         int returnVal = fDialog.showOpenDialog(null);//弹出文件选择框
 28 
 29         if(JFileChooser.APPROVE_OPTION == returnVal){
 30             pathSource = fDialog.getSelectedFile().toString();//获取文件路径并赋值给源文件路径
 31             
 32             //判断源文件路径是否正确
 33             if(!"script".equals(pathSource.substring(pathSource.length()-6,pathSource.length()))){
 34                 JOptionPane.showMessageDialog(null, "请检查您选择的文件是否正确!");
 35                 return;
 36             }
 37             
 38             String path_temp1[] = pathSource.split("\\");
 39             
 40             //组装文件地址,不包含文件名称的地址
 41             for (int i = 0; i < path_temp1.length-1; i++) {
 42                 path = path + path_temp1[i]+"\\";
 43             }
 44             //定义转换unicode编码之后的文件名称和地址 
 45             pathzh = path +"blazezh.script";
 46             //定义insert语句文件的输入地址,和文件名称
 47             pathinsert =  path +"01_SR0001_BTDS2DATA_DML_BTDS.sql";
 48             
 49         }
 50 
 51 
 52         unicodeChange(pathSource,pathzh);
 53 
 54         pickInsertSql(pathzh,pathinsert);
 55         
 56         JOptionPane.showMessageDialog(null, "转换完毕!"); 
 57 
 58     }
 59 
 60     /**
 61      * 删除转换期间产生的的无用的文件
 62      */
 63     public static void deleFile(){
 64         String pathG =  System.getProperty("user.dir");
 65         File file = new File(pathG+"\blazezh.script");   
 66         if (file.isFile() && file.exists()) {   
 67             file.delete();   
 68         }   
 69     }
 70 
 71     /**
 72      * 
 73      * 挑选出Insert语句,并写入到文件中
 74      * 
 75      */
 76     public static void pickInsertSql(String pathzh,String pathinsert) throws IOException{
 77 
 78         
 79         File write = new File(pathinsert);
 80         
 81         File file = null;
 82         BufferedReader br = null;
 83         BufferedWriter bw = new BufferedWriter(new FileWriter(write));
 84         
 85         file = new File(pathzh);
 86         br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk"));
 87         StringBuilder sb = new StringBuilder();
 88         String length = "";
 89         while ((length = br.readLine()) != null) {
 90             sb.append(length);
 91 
 92             String sql = sb.toString();
 93             sql = datestampChange(sql);
 94             sql = dateChange(sql);
 95             if("INSERT".equals(sql.substring(0,6))){
 96                 String    release_info = sql.substring(0,24);
 97                 if("INSERT INTO RELEASE_INFO".equals(release_info)){
 98 
 99                     String release_info_sub = sql.substring(0,sql.length()-1);
100                     release_info_sub = release_info_sub +","+"'PingAnProject')";
101 
102                     String []temp1 =  release_info_sub.split(",");
103                     for (int i = 0; i < temp1.length; i++) {
104                         if(i==2){
105                             temp1[2] = "'********'"; 
106                         }
107                     }
108 
109                     String insert = "";
110 
111                     for (int i = 0; i < temp1.length; i++) {
112 
113                         if(i<temp1.length-1){
114                             insert = insert + temp1[i]+",";
115                         }else{
116                             insert = insert + temp1[i];
117                         }
118                     }
119                     String []temp2 =  insert.split(",");
120                     if(temp2.length<8){
121                         bw.write("--"+insert.toString() +";"+ "
"); 
122                     }else{
123                         bw.write(insert.toString() +";"+ "
"); 
124                     }
125                 }else{
126                     bw.write(sql.toString() +";"+ "
"); 
127                 }
128                 bw.flush();
129                 sb = new StringBuilder();
130             }
131             sb = new StringBuilder();
132         }
133 
134     }
135 
136     /**
137      * 
138      * 日期转换工具,类型是yyyy-mm-dd类型
139      * 
140      */
141     
142     public static String dateChange(String temp){
143 
144         String temp2="";
145         String [] temp1 = temp.split(",");
146 
147         for (int i = 0; i < temp1.length; i++) {
148             String dateFlag = temp1[i].toString();
149             String[] datesub = dateFlag.split("-");
150             if(datesub.length>2&&dateFlag.length()<13){
151                 dateFlag = dateFlag.substring(1,11);
152                 temp1[i] = "to_date('"+dateFlag+"','yyyy-mm-dd')";
153             }
154         }
155 
156         for (int i = 0; i < temp1.length; i++) {
157             if(i<temp1.length-1){
158                 temp2 = temp2 + temp1[i]+",";
159             }else{
160                 temp2 = temp2 + temp1[i];
161             }
162         }
163 
164         return temp2;
165     }
166 
167     
168     /**
169      * 
170      * 日期转换工具,类型是yyyy-mm-dd hh24:mi:ss.ff类型
171      * 
172      */
173     public static String datestampChange(String temp){
174 
175         String temp2="";
176         String [] temp1 = temp.split(",");
177 
178         for (int i = 0; i < temp1.length; i++) {
179             String dateFlag = temp1[i].toString();
180             if(dateFlag.length()>30){
181                 dateFlag = dateFlag.substring(1,30);
182                 if("000".equals(dateFlag.substring(dateFlag.length()-3, dateFlag.length()))){
183                     String    user_favorite = temp.substring(0,25);
184                     if("INSERT INTO USER_FAVORITE".equals(user_favorite)){
185                         temp1[i] = "to_timestamp('"+dateFlag+"','yyyy-mm-dd hh24:mi:ss.ff'))";
186                     }else{
187                         temp1[i] = "to_timestamp('"+dateFlag+"','yyyy-mm-dd hh24:mi:ss.ff')";
188                     }
189 
190                 }
191             }
192         }
193 
194         for (int i = 0; i < temp1.length; i++) {
195             if(i<temp1.length-1){
196                 temp2 = temp2 + temp1[i]+",";
197             }else{
198                 temp2 = temp2 + temp1[i];
199             }
200         }
201         return temp2;
202     }
203 
204 
205     public static void unicodeChange(String path,String pathzh) throws IOException{
206 
207         File write = new File(pathzh);
208 
209         File file = null;
210         BufferedReader br = null;
211         BufferedWriter bw = new BufferedWriter(new FileWriter(write));
212         file = new File(path);
213         br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk"));
214         StringBuilder sb = new StringBuilder();
215         String length = "";
216         while ((length = br.readLine()) != null) {
217             sb.append(length);
218             bw.write(ascii2Native(sb.toString()) + "
"); 
219             bw.flush();
220             sb = new StringBuilder();
221         }
222     }
223 
224     public static String ascii2Native(String str) { 
225         StringBuilder sb = new StringBuilder(); 
226         int begin = 0; 
227         int index = str.indexOf("\u"); 
228         while (index != -1) { 
229             sb.append(str.substring(begin, index)); 
230             sb.append(ascii2Char(str.substring(index, index + 6))); 
231             begin = index + 6; 
232             index = str.indexOf("\u", begin); 
233         } 
234         sb.append(str.substring(begin)); 
235         return sb.toString(); 
236     } 
237 
238     private static char ascii2Char(String str) { 
239         if (str.length() != 6) { 
240             throw new IllegalArgumentException("长度不足6位"); 
241         } 
242         if (!"\u".equals(str.substring(0, 2))) { 
243             throw new IllegalArgumentException("字符必须以 "\u"开头."); 
244         } 
245         String tmp = str.substring(2, 4); 
246         int code = Integer.parseInt(tmp, 16) << 8; 
247         tmp = str.substring(4, 6); 
248         code += Integer.parseInt(tmp, 16); 
249         return (char) code; 
250     } 
251 
252 }
原文地址:https://www.cnblogs.com/xiayahui/p/5087744.html