package com.file;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
public class ResolveFiles {
public static String readFileContent(String filepath) {
//读取每一行保存在List 中
ArrayList<String> records=new ArrayList<String>();
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);
records.add(s); //把每条记录添加到list中
System.out.println("我 要 看 到 的:"+s);
// String bString=s.replace("/",",(?=\")|(?<=\"),"");
// System.out.println("我 要 看 到 的bString:"+bString);
if (s.indexOf("/")>0) { //把航变信息排除 (不含"/")
String[] recArray = parseRecord(s); // 按”""“ 截取
if (recArray.length>=6) { //该条记录长度
String p=recArray[6]; //得到第六个数组
ArrayList<String> records_z=new ArrayList<String>();
records_z.add(p); //把每条记录的二级第五个元素添加到list中
System.out.println("================="+recArray[4]); //得到
String[] ps=parseRecord(p);//获取票号所属引号组
//子元素:"殷俊/CA4107成都双流机场-北京首都机场2011-08-05 10:00/起飞/总票款1350.0元/保险费0.0元/票号999-2155782063/请持有效身份证件到机场办理手续,如有疑问请致电4008100999-8[中国国航]"
for (int i = 0; i < ps.length; i++) {
System.out.println("P-P-P-P-"+ps[i]+"ps.length:"+ps.length);
System.out.println("ppppp---pppppp---pppp:"+ps[i].replace("/", "\",\""));
//截断
//按“/”截取值存入数组PP中
for (int j = 0; j < ps.length; j++) {
String[] pa=subRecord(ps[j]); // 按"/" 截取
//System.out.println(""+pa[]);
System.out.println("pa:"+pa.length);
//子元素:"陈超,缪小红,尹纤黛/CA1726广州白云机场-杭州萧山机场2011-07-29 18:20/起飞/总票款2310.0元/保险费0.0元/票号999-2155782014999-2155782015999-2155782016/请持有效身份证件到机场办理手续,如有疑问请致电4008100999-8[中国国航]"
for (int k = 0; k < pa.length; k++) {
String sk=pa[k]; //定义 存储
if (sk!=null) {
if (sk.indexOf("\"")==0) { //去掉开头的引号
sk = sk.substring(1,sk.length());
}
}
pa[k]=sk;
System.out.println("二级元素(5)pa."+k+":"+pa[k]);
}
if (pa.length>=5) { //子子元素: 票号999-2155782063...
System.out.println("二级元素(5)-子元素(5)pa:"+j+i+"-----"+pa[5]);
//对票号处理 票号999-2155782014999-2155782015999-2155782016 ...
String sr=pa[5].substring(2); //截取后如:999-2155782014999-2155782015999-2155782016 ...
int begin = 0;
int end = 14;
while(end<=sr.length()) {
String b = sr.substring(begin,end);
System.out.println("==========票号截取值==========:"+b);
begin += 14;
end += 14;
}
}
}
}
/**aaaaa
* */
//处理每一条记录生成SQL语句 并保存为对象
//"梁辰/CA4194北京首都机场-成都双流机场2011-09-14 22:00/起飞/总票款1100.0元/保险费0.0元/票号999-2155782995/请持有效身份证件到机场办理手续,如有疑问请致电4008100999-8[中国国航]"
ArrayList<String> record_zList=new ArrayList<String>();
for (int i = 0; i < records_z.size(); i++) {
String record=records_z.get(i);
String[] recArray_z = minusQuotation(record.split("/"));
for (int k = 0; k < ps.length; k++) {
String[] pa_z=subRecord(ps[k]); // 按"/" 截取
if (pa_z.length>=5) { //子子元素: 票号999-2155782063...
System.out.println("2ci二级元素(5)-子元素(5)pa:"+k+"-----"+pa_z[5]);
//对票号处理 票号999-2155782014999-2155782015999-2155782016 ...
String sr=pa_z[5].substring(2); //截取后如:999-2155782014999-2155782015999-2155782016 ...
int begin = 0;
int end = 14;
while(end<=sr.length()) {
String b = sr.substring(begin,end);
System.out.println("==========票号截取值z==========:"+b);
begin += 14;
end += 14;
}
}
}
for (int j = 0; j < recArray_z.length; j++) {
System.out.println(recArray_z[j]);
}
}
}
}
/*****
*
* 下面这段不可删除 请勿动
*
*
*
int i=0;
for ( i = 0; i < recArray.length; i++) {
System.out.println("二级元素"+i+": "+recArray[i]);
String yString=recArray[i];
if (i==recArray.length-1) {
str=recArray[i];
String[] subArray=subRecord(str);
//System.out.println("eeee"+subArray);
int pi=subArray.length-2;
for(int j=0;j<subArray.length;j++){
String sa=subArray[j];
if (sa != null) {
if (sa.indexOf("\"")==0) {
sa = sa.substring(1,sa.length());//去掉开头的引号
}
}
subArray[j]=sa;
System.out.println("二级元素(5)-子元素:"+j+":"+subArray[j]);
if (j==subArray.length-7) {
String subString=subArray[j];
String[] ssubArray=ssubRcord(subString);
for (int k = 0; k < ssubArray.length; k++) {
String st=ssubArray[k];
if (st != null) {
if (st.indexOf("\"")==0) {
st=st.substring(1,st.length());
}
}
ssubArray[k]=st;
System.out.println("==========二级元素(5)-子元素(0)k=========="+k+":"+ssubArray[k]);
}
System.out.println("yString:"+yString);
}
}
}
}
*
* 此段不可删除 请勿动
*
*/
}
} 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("/");//按斜杠截取
return subArray;
}
public static String[] ssubRcord(String sstr){
String[] ssubArray=sstr.split(",");//按逗号截取
return ssubArray;
}
/*
* 截取票号方法
* */
public static String[] subStrings(String sstrr){ //对票号截取处理
//String string="票号999-2155782014999-2155782015999-2155782016";
//String str="999-";
String sr=sstrr.substring(2);
int begin = 0;
int end = 14;
while(end<=sr.length()) {
String b = sr.substring(begin,end);
System.out.println(b);
begin += 14;
end += 14;
}
return null;
}
//批量执行SQL
public static int batchExecuteSql(ArrayList<String> sqlList) {
System.out.println("接下来可以执行SQL语句或保存对象");
System.out.println("========批量执行SQL语句==========");
System.out.println("将所有语句加入到Statment stat中");
for (int i = 0;i<sqlList.size();i++) {
String string = sqlList.get(i);
System.out.println("通过stat.addBatch(sql)来加入语句"+i+": '"+string+"'");
}
System.out.println("通过stat.executeBatch()来执行所有的SQL语句");
System.out.println("========批量执行SQL语句结束==========");
//int count = stat.executeBatch();
//return count;//返回执行的语句数量
return sqlList.size();
}
//生成每条记录的SQL
public static String getRecordSql(String[] recArray) {
if (null==recArray) {
return null;
}
String recordSql = "insert into tablename (sms,no,time) values('"+recArray[0]+"','"+recArray[2]+"','"+recArray[5]+"')";
return recordSql;
}
/**
* 去掉数组中每个元素的开头和结尾的引号
*
* */
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 void main(String[] args) {
String filepath = "E:\\sxySMS\\smstest(1).txt";
readFileContent(filepath);
String s="票号999-2155782014999-21557820,15999-2155782016";
String[] b=s.split(",");
for (int i = 0; i < b.length; i++) {
System.out.println(b[i]);
}
}
}
txt解析经典测试(2)
最新推荐文章于 2024-05-09 23:18:26 发布