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

txt文件上传数据 批量解析批量存入数据库方法操作(1)

	/**  
         *常用方法
	 * 去掉数组中每一个元素的开头和结尾的引号
	 * @param recArray 要处理的数组
	 * @return 处理后的数组
	 */
	public static String[] minusQuotation(String[] recArray) {
		
		for (int i = 0; i < recArray.length; i++) {
			String str = recArray[i];
			if (null!=str) {
				if(str.indexOf( "\"")==0)
					str = str.substring(1,str.length());//去掉开头的分号
				if(str.lastIndexOf("\"")==(str.length()-1))
					str = str.substring(0,str.length()-1); //去掉最后的分号
			}
			recArray[i] = str;
		}
		return recArray;
	}
	
	
	//按引号截取  
    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("/");//按斜杠截取  
        return subArray;  
    }  
    //按逗号截取  
    public static String[] ssubRcord(String sstr){  
        String[] ssubArray=sstr.split(",");//按逗号截取  
        return ssubArray;  
          
    }  
   





/**
	 * 
	 * 文件解析中批量解析方法   (批量提交txt文件信息解析)
	 * 
	 * */
	public static String ResolveFileContent(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;  //每条数据   
	            ArrayList<String[]> recList=new ArrayList<String[]>();  //存储所有解析后的集合
	            while((s = br.readLine())!=null){  
	                //System.out.println("数据记录为:"+s);  
	            System.out.println("000=====================================================================");
	            if (s.indexOf("/")>0) {  //把航变信息排除  (不含"/")
	            	
	            	String aString=s.replace("/", "\",\"");  //将每行内涵有的 (/) 转为(",")
	            	System.out.println("ee"+aString);
	            	String[] recArray = parseRecord(aString);  // 按”""“ 截取  
	            
	                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]);
						}
	                	
	                	
	                	if (mosStrings[6].split(",").length>0) { //mosStrings[6]乘客姓名在该行记录里面存在一个以上的
	                			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 接收每条数据进行处理存储
								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());
										 
								 }
						         recList.add(strings);  //将每条经过处理的数组 添加到集合
								 System.out.pr