日期:2014-05-16  浏览次数:20492 次

读取本地文件解析txt文件 存于数据库(测试类)
  解析测试类:

   package com.file;

import java.io.BufferedReader;

import java.io.File;
import java.io.FileInputStream;

import java.io.InputStreamReader;
import java.util.ArrayList;

import org.dom4j.Branch;

import com.service.iservice.IFlightChangeInfoService;

public class ResolveFlightInfo {

	
	/**
	 * 航班信息解析测试java类
	 * 
	 * */
    public static String readFileContent(String filepath) {  
        try {  
        
            BufferedReader br = null; 
//          new BufferedReader(new FileReader(filepath));  
            InputStreamReader reader = new InputStreamReader(new FileInputStream(new File(filepath)),"GB2312");
            br = new BufferedReader(reader);
            //基元素: "sms,deliver,"10657502022321","","","2011.07.28 11:04","","彭元彬,朱宇英/CA1509北京首都机场-杭州萧山机场2011-07-31 07:55/起飞/总票款1420.0元/保险费0.0元/票号999-2155782847999-2155782848/请持有效身份证件到机场办理手续,如有疑问请致电4008100999-8[中国国航]"  
            String s,str;  //每条数据   
            
            while((s = br.readLine())!=null){  
                //System.out.println("数据记录为:"+s);  
            ArrayList<String[]> recList=new ArrayList<String[]>();  //存储所有解析后的集合
            if (s.indexOf("/")>0) {  //把航变信息排除  (不含"/")
            	
            	String aString=s.replace("/", "\",\"");  //将每行内涵有的 (/) 转为(",")
            	System.out.println("ee"+aString);
            	


              
            	
                String[] recArray = parseRecord(aString);  // 按”""“ 截取  
            	System.out.println(recArray.length+"////////");
               
                
                if (recArray.length>=1) {  //该条记录长度  
                	
                	for (int i = 0; i < recArray.length; i++) {

						System.out.println("||||||||||||||||||"+recArray[i]);
					}
                	String[] mosStrings=minusQuotation(recArray);
                	for (int i = 0; i < mosStrings.length; i++) {
                		System.out.println("-----------------"+mosStrings[i]);
					}
                	//String[] kStrings=null;
                	
                	if (mosStrings[6].split(",").length>0) { //说明存在一条以上
                			int begin = 0;  
                	        int end = 14;  
                		for (int i = 0; i < mosStrings[6].split(",").length; i++) {
							String ph=mosStrings[11].substring(2); //截取票号数组串
							while(end<=ph.length()) {  //判断票号长度截取对应票号值
							//数组strings 接收每条数据进行处理存储
								System.out.println(":mosSt:"+mosStrings[6].split(",").length);
							
							String[] strings={mosStrings[0],mosStrings[1],mosStrings[2],mosStrings[3],mosStrings[4],mosStrings[5],mosStrings[6].split(",")[i++],mosStrings[7],mosStrings[8],mosStrings[9].substring(3,mosStrings[9].length()-1),mosStrings[10].substring(3,mosStrings[10].length()-1),mosStrings[11].substring(2).substring(begin,end),mosStrings[12]};
							
							 begin += 14;  //票号增值
					         end += 14;  
							
					         
					        
					         for (int j = 0; j < strings.length; j++) {  //
					        	 System.out.println("================要存入数据的数组=====================:"+j+":"+strings[j]+"==and=="+strings[j].length());
					        	
					         }
					         System.out.println("====================================================================");
					         recList.add(strings);  //将每条经过处理的数组 添加到集合
				        	
							
							}
						}
					}
                }
                
             }
            System.out.println("recList.size:"+recList.size());
            }
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return null;  
    }  
    public static String[] parseRecord(String record) {      
        String[] recArray = record.split(",(?=\")|(?<=\"),");//按引号截取  
  
        //String[] recArray2 = record.  
        return recArray;  
    }  
    public static String[] subRecord(String subrString){  
        String[] subArray=subrString.split("/");//按斜杠截取