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

时间:2021-04-11 13:10:34
 package com;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader; import javax.swing.JFileChooser;
import javax.swing.JOptionPane; public class supZhtool_1 { public static void main(String[] args) throws IOException { String path ="";//源文件路径,不包括文件名称
String pathzh = "";//转换unicode编码后文件输出路径
String pathinsert = "";//筛选出insert语句后的文件输出路径
String pathSource = "";//源文件路径,包含文件名称的源文件路径 JFileChooser fDialog = new JFileChooser(); fDialog.setDialogTitle("请选择需要转换的文件"); int returnVal = fDialog.showOpenDialog(null);//弹出文件选择框 if(JFileChooser.APPROVE_OPTION == returnVal){
pathSource = fDialog.getSelectedFile().toString();//获取文件路径并赋值给源文件路径 //判断源文件路径是否正确
if(!"script".equals(pathSource.substring(pathSource.length()-6,pathSource.length()))){
JOptionPane.showMessageDialog(null, "请检查您选择的文件是否正确!");
return;
} String path_temp1[] = pathSource.split("\\\\"); //组装文件地址,不包含文件名称的地址
for (int i = 0; i < path_temp1.length-1; i++) {
path = path + path_temp1[i]+"\\\\";
}
//定义转换unicode编码之后的文件名称和地址
pathzh = path +"blazezh.script";
//定义insert语句文件的输入地址,和文件名称
pathinsert = path +"01_SR0001_BTDS2DATA_DML_BTDS.sql"; } unicodeChange(pathSource,pathzh); pickInsertSql(pathzh,pathinsert); JOptionPane.showMessageDialog(null, "转换完毕!"); } /**
* 删除转换期间产生的的无用的文件
*/
public static void deleFile(){
String pathG = System.getProperty("user.dir");
File file = new File(pathG+"\\blazezh.script");
if (file.isFile() && file.exists()) {
file.delete();
}
} /**
*
* 挑选出Insert语句,并写入到文件中
*
*/
public static void pickInsertSql(String pathzh,String pathinsert) throws IOException{ File write = new File(pathinsert); File file = null;
BufferedReader br = null;
BufferedWriter bw = new BufferedWriter(new FileWriter(write)); file = new File(pathzh);
br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk"));
StringBuilder sb = new StringBuilder();
String length = "";
while ((length = br.readLine()) != null) {
sb.append(length); String sql = sb.toString();
sql = datestampChange(sql);
sql = dateChange(sql);
if("INSERT".equals(sql.substring(0,6))){
String release_info = sql.substring(0,24);
if("INSERT INTO RELEASE_INFO".equals(release_info)){ String release_info_sub = sql.substring(0,sql.length()-1);
release_info_sub = release_info_sub +","+"'PingAnProject')"; String []temp1 = release_info_sub.split(",");
for (int i = 0; i < temp1.length; i++) {
if(i==2){
temp1[2] = "'********'";
}
} String insert = ""; for (int i = 0; i < temp1.length; i++) { if(i<temp1.length-1){
insert = insert + temp1[i]+",";
}else{
insert = insert + temp1[i];
}
}
String []temp2 = insert.split(",");
if(temp2.length<8){
bw.write("--"+insert.toString() +";"+ "\r\n");
}else{
bw.write(insert.toString() +";"+ "\r\n");
}
}else{
bw.write(sql.toString() +";"+ "\r\n");
}
bw.flush();
sb = new StringBuilder();
}
sb = new StringBuilder();
} } /**
*
* 日期转换工具,类型是yyyy-mm-dd类型
*
*/ public static String dateChange(String temp){ String temp2="";
String [] temp1 = temp.split(","); for (int i = 0; i < temp1.length; i++) {
String dateFlag = temp1[i].toString();
String[] datesub = dateFlag.split("-");
if(datesub.length>2&&dateFlag.length()<13){
dateFlag = dateFlag.substring(1,11);
temp1[i] = "to_date('"+dateFlag+"','yyyy-mm-dd')";
}
} for (int i = 0; i < temp1.length; i++) {
if(i<temp1.length-1){
temp2 = temp2 + temp1[i]+",";
}else{
temp2 = temp2 + temp1[i];
}
} return temp2;
} /**
*
* 日期转换工具,类型是yyyy-mm-dd hh24:mi:ss.ff类型
*
*/
public static String datestampChange(String temp){ String temp2="";
String [] temp1 = temp.split(","); for (int i = 0; i < temp1.length; i++) {
String dateFlag = temp1[i].toString();
if(dateFlag.length()>30){
dateFlag = dateFlag.substring(1,30);
if("000".equals(dateFlag.substring(dateFlag.length()-3, dateFlag.length()))){
String user_favorite = temp.substring(0,25);
if("INSERT INTO USER_FAVORITE".equals(user_favorite)){
temp1[i] = "to_timestamp('"+dateFlag+"','yyyy-mm-dd hh24:mi:ss.ff'))";
}else{
temp1[i] = "to_timestamp('"+dateFlag+"','yyyy-mm-dd hh24:mi:ss.ff')";
} }
}
} for (int i = 0; i < temp1.length; i++) {
if(i<temp1.length-1){
temp2 = temp2 + temp1[i]+",";
}else{
temp2 = temp2 + temp1[i];
}
}
return temp2;
} public static void unicodeChange(String path,String pathzh) throws IOException{ File write = new File(pathzh); File file = null;
BufferedReader br = null;
BufferedWriter bw = new BufferedWriter(new FileWriter(write));
file = new File(path);
br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "gbk"));
StringBuilder sb = new StringBuilder();
String length = "";
while ((length = br.readLine()) != null) {
sb.append(length);
bw.write(ascii2Native(sb.toString()) + "\r\n");
bw.flush();
sb = new StringBuilder();
}
} public static String ascii2Native(String str) {
StringBuilder sb = new StringBuilder();
int begin = 0;
int index = str.indexOf("\\u");
while (index != -1) {
sb.append(str.substring(begin, index));
sb.append(ascii2Char(str.substring(index, index + 6)));
begin = index + 6;
index = str.indexOf("\\u", begin);
}
sb.append(str.substring(begin));
return sb.toString();
} private static char ascii2Char(String str) {
if (str.length() != 6) {
throw new IllegalArgumentException("长度不足6位");
}
if (!"\\u".equals(str.substring(0, 2))) {
throw new IllegalArgumentException("字符必须以 \"\\u\"开头.");
}
String tmp = str.substring(2, 4);
int code = Integer.parseInt(tmp, 16) << 8;
tmp = str.substring(4, 6);
code += Integer.parseInt(tmp, 16);
return (char) code;
} }