java转换unicode,筛选文件中的insert语句并把日期给转换为可以直接在数据库执行的语句
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;
} }
java转换unicode,筛选文件中的insert语句并把日期给转换为可以直接在数据库执行的语句的更多相关文章
- java代码将excel文件中的内容列表转换成JS文件输出
思路分析 我们想要把excel文件中的内容转为其他形式的文件输出,肯定需要分两步走: 1.把excel文件中的内容读出来: 2.将内容写到新的文件中. 举例 一张excel表中有一个表格: 我们需要将 ...
- Linux下执行的java命令重定向到文件中的方法
在Linux下通常会执行如:java -version 的命令, 但是,命令只是打印到了屏幕上不能重定向到文件中或标准输出流中. 此时需要将错误输出流重定向到标准输出流中就可以得到了. 比如:java ...
- Java IO把一个文件中的内容以字符串的形式读出来
代码记录(备查): /** * 把一个文件中的内容以字符串的形式读出来 * * @author zhipengs * */ public class FileToString { public sta ...
- Mybatis实体类的映射文件中select,insert语句使用
id:在命名空间中唯一的标识符,可以被用来引用这条语句. parameterType:设置传入这条语句的参数的数据类型,如int,String...... resultType:设置从这条语句中返回数 ...
- java使用ObjectInputStream从文件中读取对象
import java.io.EOFException;import java.io.FileInputStream;import java.io.FileNotFoundException;impo ...
- JAVA 中文 unicode 相互转换 文件读取
package com.test; import org.junit.Test; public class JunitTest { @Test public void test(){ String p ...
- 【反射】利用java反射原理将xml文件中的字段封装成对应的Bean
本例使用的xml解析方式为jdom ... <ROOT> <Consignment> ... </Consignment> </ROOT> 解析xml文 ...
- Java程序栈信息文件中的秘密(五)
最近发现在使用jstack工具在导出Java应用的线程栈时有一个小小的窍门,比如Linux环境上有一个用户为appuser,假如以这个用户启动了一个Java进程B,如果想要导出进程B的线程栈,则必须切 ...
- SQL Server清理大日志文件方法 不分离数据库 执行SQL语句即可
SQL 2008清空日志的SQL语句如下: USE[master] GO ALTER DATABASE 要清理的数据库名称 SET RECOVERY SIMPLE WITH NO_WAIT GO AL ...
随机推荐
- spring security 控制用户信息用户加密 缓存用户信息
1. MD5加密 任何一个正式的企业应用中,都不会在数据库中使用明文来保存密码的,我们在之前的章节中都是为了方便起见没有对数据库中的用户密码进行加密,这在实际应用中是极为幼稚的做法.可以想象一下,只要 ...
- c语言.大数的输出
转化成字符串,再用for循环输出: #include <stdio.h>#include <string.h>int main(){ char s[32]; int d, ...
- 杭电ACM 1013 Digital Root
#include<stdio.h>#include<stdlib.h>#include<string.h>int main(){char s[100000];int ...
- 关于static 的研究 与递归调用的输出
static的作用 :1.保存上次执行的结果 2.static int m; 这里默认m的初始值为0,即默认 值是0 #include "stdio.h" int fun(int ...
- DHTML概述
<!-- DHTML概述:动态的HTML.不是一门语言,是多项技术综合体的简称. 其中包含了HTML.CSS.DOM.JavaScript. 这四个技术在动态HTML页面效果定义时,都处于什么样 ...
- Educational Codeforces Round 16 A B C E
做题太久也有点累了..难题不愿做 水题不愿敲..床上一躺一下午..离下一场div2还有点时间 正好有edu的不计分场 就做了一下玩玩了 D是个数学题 F是个AC自动机 都没看明白 留待以后补 A 给出 ...
- [archlinux][hardware] 查看SSD的使用寿命
因为最近把16GB的SSD做成了HDD的cache,所以比较关系寿命问题. 使用smartctl工具. 参考:https://www.v2ex.com/t/261373 linux 下面只有 smar ...
- [LeetCode]题解(python):118 Pascal's Triangle
题目来源 https://leetcode.com/problems/pascals-triangle/ Given numRows, generate the first numRows of Pa ...
- windows下MySQL更改数据库文件目录及1045,1067错误
MySQL安装时不能选择数据库文件的安装位置, 也没有可用的直接更改数据库目录的工具,要想更改数据目录,方法如下: MySQL安装并配置完毕,默认的数据哭安装目录为 C:/ProgramData/My ...
- iOS ARC中CTCallCenter没用,无法监听电话的解决方案
今天在尝试使用CTCallCenter进行电话监听时,发现一直无法捕获电话状态改变的事件,研究了一番之后找到了解决方案,在这里分享给大家. 首先使用CTCallCenter监听电话的代码如下: CTC ...