Java 如何在将文件从sql转换为tbl时删除引号

Java 如何在将文件从sql转换为tbl时删除引号,java,eclipse,Java,Eclipse,下面是将insert语句的.sql文件转换为.tbl文件的代码。由于输入文件是.sql文件,因此在单引号内包含varchar(字符串)值。如何防止这些引用进入我的tbl文件 输入: INSERT INTO post_tran(post_tran_id,tran_nr,datetime_tran_local,system_trace_audit_nr,settle_ amount_rsp,settle_tran_fee_rsp,post_tran_cust_id,prev_post_tran_i

下面是将insert语句的.sql文件转换为.tbl文件的代码。由于输入文件是.sql文件,因此在单引号内包含varchar(字符串)值。如何防止这些引用进入我的tbl文件

输入:

INSERT INTO 
post_tran(post_tran_id,tran_nr,datetime_tran_local,system_trace_audit_nr,settle_
amount_rsp,settle_tran_fee_rsp,post_tran_cust_id,prev_post_tran_id,next_post_tra
n_id,message_type,tran_postilion_originated,sink_node_name,settle_entity_id,batc
h_nr,tran_completed,tran_type,rsp_code_rsp,auth_type,auth_reason,retrieval_refer
ence_nr,settle_amount_req,settle_tran_fee_req,settle_currency_code,datetime_req,
recon_business_date,realtime_business_date,acquiring_inst_id_code) VALUES( 
1,2,'2002-04-02 19:02:28','008497',4120,10, 2, 0,0,'0200', 0, 'LinkSink', 
1,1,0,'01','01','00',0,'000000000102',6000,0,'840', '', '2004-02-11 
12:00:00', '2004-02-11 12:00:00', '2200017000')
例如:
处理接收器节点名称时,其数据值应为
LinkSink
,而不是
'LinkSink'

public class SqlToTblCoverter {

    private File source_folder = null;
    private File destination_folder = null;
    private String source_absolute_path = null;
    private String destination_absolute_path = null;
    private String absolute_file_name = null;
    private String absolute_new_file_name = null;
    private List<String> column_name_list = null;
    private List<String> column_values_list = null;

    public SqlToTblCoverter(String source_folder_name,
            String destination_folder_name) {
        source_folder = new File(source_folder_name);
        destination_folder = new File(destination_folder_name);
        source_absolute_path = source_folder.getAbsolutePath();
        destination_absolute_path = destination_folder.getAbsolutePath();
        column_name_list = new ArrayList<String>();
        column_values_list = new ArrayList<String>();
    }

    public void run() throws IOException {
        validateInputs();
        migrateFiles();
    }

    private void validateInputs() {
        if (source_folder.isDirectory() == false) {
            System.out.println("Source must be a FOLDER");
        } else if (destination_folder.isDirectory() == false) {
            System.out.println("Destination must be a FOLDER");
        }
    }

    private void migrateFiles() throws IOException {
        String[] file_list = source_folder.list();
        String file_name1 = file_list[0];
        // System.out.println(file_name1);

        String file_name2 = file_list[1];
        // System.out.println(file_name2);

        String f1 = migrateFileContains(file_name1);
        String f2 = migrateFileContains(file_name2);

        Migrator mg = new Migrator();
        mg.migrate(f1, f2);
    }

    private String migrateFileContains(String file_name) throws IOException {
        absolute_file_name = source_absolute_path + File.separator + file_name;
        absolute_new_file_name = destination_absolute_path + File.separator
                + getNewFileName(file_name);
        BufferedReader br = new BufferedReader(new InputStreamReader(
                new FileInputStream(new File(absolute_file_name))));
        String line_info = br.readLine();
        StringBuffer new_query = new StringBuffer("");

        FileWriter fw = new FileWriter(new File(absolute_new_file_name));
        while (line_info != null) {
            String convertQuery = convertQuery(line_info);
            if (convertQuery.isEmpty()) {
                line_info = br.readLine();
                continue;
            }
            new_query.append(convertQuery);
            new_query.append(System.getProperty("line.separator"));
            fw.write(new_query.toString());
            new_query.setLength(0);
            line_info = br.readLine();
        }
        br.close();
        fw.close();
        return absolute_new_file_name;

    }

    private String convertQuery(String query) {
        String new_query = "";
        if (query.startsWith("INSERT")) {
            int round_bracket_start = query.indexOf('(');
            int round_bracket_end = query.indexOf(')');
            int round_bracket_start_after_values = query.indexOf('(',
                    round_bracket_end);
            String query_column_name = query.substring(round_bracket_start + 1,
                    round_bracket_end);
            String query_column_values = query.substring(
                    round_bracket_start_after_values + 1, query.length() - 1);
            covertColumnNameList(query_column_name);
            covertColumnValueList(',' + query_column_values + ',');
            new_query = createNewQuery() + "\n";
        }
        column_name_list.clear();
        column_values_list.clear();
        return new_query;
    }

    private void covertColumnNameList(String query_column_name) {
        String[] column_list = query_column_name.split(",");
        for (String column_name : column_list) {
            column_name_list.add(column_name);
        }
    }

    private void covertColumnValueList(String query_column_values) {
        if (query_column_values.equals(",")) {
            return;
        }
        String column_value = null;
        int comma_index = query_column_values.indexOf(',');
        int next_comma_index = 0;
        if (query_column_values.charAt(comma_index + 1) == '\'') {
            int quote_index = query_column_values.indexOf('\'', comma_index);
            int next_quote_index = query_column_values.indexOf('\'',
                    quote_index + 1);
            next_comma_index = query_column_values.indexOf(',',
                    next_quote_index);
            column_value = query_column_values.substring(comma_index + 2,
                    next_comma_index - 1);
        } else {
            next_comma_index = query_column_values
                    .indexOf(',', comma_index + 1);
            column_value = query_column_values.substring(comma_index + 1,
                    next_comma_index);
        }
        column_values_list.add(column_value);
        covertColumnValueList(query_column_values.substring(next_comma_index));
    }

    private String createNewQuery() {
        StringBuffer buffer = new StringBuffer("");
        if (column_name_list.size() != column_values_list.size()) {
            System.out.println("Error : " + absolute_file_name);
        } else {
            for (int index = 0; index < column_name_list.size(); index++) {
                buffer.append(createNewColumn(column_name_list.get(index),
                        column_values_list.get(index)));
            }
        }
        return buffer.toString();
    }

    private String createNewColumn(String column_name, String column_value) {
        StringBuffer buffer = new StringBuffer("");
        buffer.append("[name]".trim());
        buffer.append(column_name.trim());
        buffer.append("[/name]=[data]".trim());
        buffer.append(column_value.trim());
        buffer.append("[/data]".trim());
        buffer.append("\r\n");
        return buffer.toString();
    }

    private String getNewFileName(String file_name) {
        String new_file_name = "";
        int dot_index = file_name.indexOf('.');
        new_file_name = file_name.subSequence(0, dot_index) + ".tbl";
        return new_file_name;
    }
}
公共类SqlToTblCoverter{
私有文件源\文件夹=null;
私有文件目的地\u文件夹=null;
私有字符串源\绝对\路径=null;
私有字符串目的地\绝对\路径=空;
私有字符串绝对文件名=null;
私有字符串绝对值\新建\文件\名称=空;
私有列表列\u name\u List=null;
私有列表列\u值\u列表=空;
公共SqlToTblCoverter(字符串源\文件夹\名称,
字符串目的地(文件夹名称){
source\u folder=新文件(source\u folder\u名称);
目标文件夹=新文件(目标文件夹名称);
source_absolute_path=source_folder.getAbsolutePath();
destination_absolute_path=destination_文件夹。getAbsolutePath();
column_name_list=new ArrayList();
列值列表=新的ArrayList();
}
public void run()引发IOException{
验证输入();
迁移文件();
}
私有void validateInputs(){
if(source_folder.isDirectory()==false){
System.out.println(“源必须是文件夹”);
}else if(destination_folder.isDirectory()==false){
System.out.println(“目标必须是文件夹”);
}
}
私有void migrateFiles()引发IOException{
String[]file_list=source_folder.list();
字符串file_name1=文件列表[0];
//System.out.println(文件名1);
字符串file_name2=文件列表[1];
//System.out.println(文件名2);
字符串f1=migrateFileContains(文件名1);
字符串f2=migrateFileContains(文件名2);
Migrator mg=新的Migrator();
迁移(f1,f2);
}
私有字符串migrateFileContains(字符串文件名)引发IOException{
绝对文件名=源绝对路径+文件名+分隔符+文件名;
绝对\新\文件\名称=目标\绝对\路径+文件.separator
+getNewFileName(文件名);
BufferedReader br=新的BufferedReader(新的InputStreamReader(
新文件输入流(新文件(绝对文件名));
字符串行_info=br.readLine();
StringBuffer new\u query=newStringBuffer(“”);
FileWriter fw=新的FileWriter(新文件(绝对新文件名));
while(行信息!=null){
字符串convertQuery=convertQuery(行信息);
if(convertQuery.isEmpty()){
line_info=br.readLine();
继续;
}
new_query.append(convertQuery);
new_query.append(System.getProperty(“line.separator”);
write(new_query.toString());
new_query.setLength(0);
line_info=br.readLine();
}
br.close();
fw.close();
返回绝对\u新\u文件\u名称;
}
私有字符串查询(字符串查询){
字符串new_query=“”;
if(query.startsWith(“插入”)){
int round_bracket_start=query.indexOf('(');
int round_方括号_end=query.indexOf(');
int round_bracket_start_在_values=query.indexOf('(')之后,
圆形(支架端部);
String query\u column\u name=query.substring(圆括号\u start+1,
圆形(支架端部);
字符串查询\列\值=query.substring(
_值+1,query.length()-1)后的圆括号\u start\u;
covertColumnNameList(查询列名称);
covertColumnValueList(“,”+查询_列_值+”,”);
new\u query=createNewQuery()+“\n”;
}
列名称列表。清除();
列值列表。清除();
返回新的查询;
}
private void covertColumnNameList(字符串查询\列\名称){
String[]column\u list=query\u column\u name.split(“,”);
用于(字符串列名称:列列表){
列名称列表。添加(列名称);
}
}
private void covertColumnValueList(字符串查询\列\值){
if(查询列值等于(“,”)){
返回;
}
字符串列_值=null;
int comma_index=query_column_values.indexOf(',');
int next_逗号_索引=0;
如果(查询列值.charAt(逗号索引+1)='\''){
int quote\u index=query\u column\u values.indexOf('\'',逗号索引);
int next\u quote\u index=query\u column\u values.indexOf('\'',
报价(指数+1);
下一个逗号索引=查询列值,
下一步(引用索引);
列值=查询列值。子字符串(逗号索引+2,
下一个(逗号索引-1);
}否则{
下一步\逗号\索引=查询\列\值
.indexOf(“,”,逗号索引+1);
列值=查询列值。子字符串(逗号索引+1,
下一步(逗号索引);
}
列值列表。添加(列值);
covertColumnValueList(查询列值.子字符串(下一个逗号索引));
}
私有字符串createNewQuery(){
StringBuffer=新的StringBuffer(“”);
if(column\u name\u list.size()!=column\u values\u list.size()){
System.out.println(“错误:+绝对文件名”);
}否则{
对于(int index=0;index//....
int next_comma_index = 0;
// skip space(s)
while( query_column_values.charAt(comma_index + 1) == ' ' ){
    comma_index++;
}
if (query_column_values.charAt(comma_index + 1) == '\'') {
    //...
private void covertColumnValueList(String query_column_values) {
if (query_column_values.equals(",")) {
    return;
}
String column_value = null;
int comma_index = query_column_values.indexOf(',');
int next_comma_index = 0;
if (query_column_values.charAt(comma_index + 1) == '\'') {
    int quote_index = query_column_values.indexOf('\'', comma_index);
    int next_quote_index = query_column_values.indexOf('\'',
            quote_index + 1);
    next_comma_index = query_column_values.indexOf(',',
            next_quote_index);
    column_value = query_column_values.substring(comma_index + 2,
            next_comma_index - 1);
 column_value=column_value.replace("\'","") ;
} else {
    next_comma_index = query_column_values
            .indexOf(',', comma_index + 1);
    column_value = query_column_values.substring(comma_index + 1,
            next_comma_index);
   column_value=column_value.replace("\'","") ;
}
column_values_list.add(column_value);
covertColumnValueList(query_column_values.substring(next_comma_index));
}
private void covertColumnValueList(String query_column_values) {
    String temp_val = null;

    for (String col_val : query_column_values.split(",")) {

        if (col_val.contains("\'")) {
            temp_val = col_val.replaceAll("\'", "");
            column_values_list.add(temp_val.trim());
        } else
            column_values_list.add(col_val.trim());
    }
}
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");        
Calendar cal = Calendar.getInstance();
column_values_list.set( 23, dateFormat.format(cal.getTime()));