我有一个.txt的文本文件,里面存储了上千条记录,格式都是这样的。
294991@29499@Linked@Li@000194704
@306831@30683@Duplicate@Doublon@000301702
@353061@35306@Duplicate@Doublon@000334983
@411441@41144@Duplicate@Doublon@000224202
@441591@44159@Duplicate@Doublon@000046307
@463071@46307@Duplicate@Doublon@000044159...我有一张表,里面有五个字段,就是文本里对应的这五个,它们之间已经用@符号分割开了。我现在想要分别取出每个字段,最后用JDBC(或者别的方式也行)把所有的数据insert into到表中,请教怎样实现这个过程?谢谢~

解决方案 »

  1.   

    如果只是一次整理 的话 建议 用EXCEL处理
      

  2.   

    1、读取每行记录
    2、取得记录行以@分割并存于一个数组中
    3、存储数组中的每个元素1、2、3同时放在一个循环中执行,并记得用PreparedStatement的addBatch()批处理方法,这样效率要高很多很多,addBatch()如果是用于存储到本地,批执行语句几乎无上限,如果是网络存储则根据带宽上限大约150到200左右。
      

  3.   

    如果是sqlserver 用文本编辑器打开替换@为逗号,文件另存为csv,直接可以导入
      

  4.   

    package com.briup.woss.client.impl;import java.io.*;
    import java.sql.Timestamp;
    import java.util.*;
    import java.util.regex.*;import com.briup.common.proj.BIDR;
    import com.briup.woss.client.IGather;public class Gather implements IGather { static Set<BIDR>bidrSet1 =Collections.synchronizedSet(new HashSet<BIDR>());
    static Set<BIDR>bidrSet2 =Collections.synchronizedSet(new HashSet<BIDR>());
    static Set<BIDR>bidrSet =Collections.synchronizedSet(new HashSet<BIDR>());

    static Map<String,String> pointMap =new HashMap<String,String>();  //从文件中拿取count数量的信息
    static Map<String,String> allMap =new HashMap<String,String>();    //保存所有上线下线信息 
    static Map<String,String> upMap =new HashMap<String,String>();     //从文件中拿取未下线信息


    final static long count=10000;
    static final long time = 1000;

    static long point = 0;
    static long prePoint = 0;
    static int length=0;
    static int preLength = 0;
    int find = 1;

    boolean flag = false;      //鉴别解析文件中未下线信息
    boolean flag1 = true;      //控制保存没下线信息线程的关闭

    static BufferedReader br = null;
    static String path;

    static{
    try{
    //point = getPoint();
    //initData();

    }catch(Exception e){
    e.printStackTrace();
    }
    }

    @Override
    public Collection<BIDR> gather() throws Exception {
    new ParseFile().start();           //解析全部文件
            new ParseUpset().start();          //解析上线文件
            new refreshUpData().start();       //保存没下线信息
            
    Thread.currentThread().sleep(time);    //调用gather()时为了确保线程已经完成采集,等待几秒钟在返回给客户端发送
    if(bidrSet.size()!=0) {  
    for(BIDR bidr1:bidrSet1){
    bidrSet2.add(bidr1);
    }
    }
    bidrSet = bidrSet2;                    //将两个线程采集的数据汇总
                        //System.out.println("共封装:"+bidrSet.size());
        return  bidrSet;
    } @Override
    public void setProperties(Properties prop) {
    Set<Object> keys = prop.keySet();
      for(Object o:keys){
      String key = (String) o;
      String value = prop.getProperty(key);
      this.prop.setProperty(key, value);
               
      }
      path = prop.getProperty("pointer-size");
      point = getPoint();
      prePoint = point;
      initData();
      
      //System.out.println(this.prop);
    } private static  long getPoint(){
    try{
       br  = new BufferedReader(new InputStreamReader(new FileInputStream(path)));

       String data = br.readLine();
       point = Integer.parseInt(data);
       if(point>=19495){
       point = 1;
       }
                              System.out.println("当前读取位置:"+point);
       
    }catch(Exception e){
    e.printStackTrace();
    }finally{
    try {
    br.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    return point;
    }

    public static void test(){
    System.out.println("下线信息:"+allMap.size());
    System.out.println("当前上线信息:"+pointMap.size());
    }

    private static   void initData(){
    try {
    long p = 1;
    String pathup = prop.getProperty("up-file");
    BufferedReader brup  = new BufferedReader(new InputStreamReader(
                       new FileInputStream(pathup)));
    String strup = brup.readLine();
    while(strup!=null){
    String s[] = strup.split("\\|");
    upMap.put(s[2],strup);
    strup = brup.readLine();
          // System.out.println(upset);
           
    }

    if(upMap.size()==0)            System.out.println("上次没显现信息被破坏,或不存在");


    String path = prop.getProperty("src-file");
    br  = new BufferedReader(new InputStreamReader(new FileInputStream(path)));

    String data = br.readLine();
    while(data!=null&&p<=point){
    data = br.readLine();
    p++;                         //System.out.println("当前读取位置:"+p);
    }
    //p = 0;
    while(p<=(point+count)&&data!=null){
    String s[] = data.split("\\|");
    if("7".equals(s[2]))  
    pointMap.put(s[4],data);    //System.out.println("当前读数据:"+p+1+data+"set.size:"+set.size());
    if("8".equals(s[2])){
        allMap.put(s[4],data);
    }
    p++;
    data = br.readLine(); 
    }
                                  //System.out.println("当前读数据:"+count);
        while(data!=null){   
         String s[] = data.split("\\|");
         if("8".equals(s[2])){
        allMap.put(s[4],data);
    }
        p++;
         data = br.readLine();
        }                           
                                    //System.out.println("当前加载上下线信息:"+allMap.size());
                                   // System.out.println();
      point+=count;                 
      if(point>p) point = p;        System.out.println("加载后位置:"+point);
      length =  upMap.size();                           //////////////////test();//////////////////////////////
      preLength = length;
    }catch(Exception e){
    e.printStackTrace();
    refreshPoint(prePoint);
    }finally{
    try {
    br.close();
    } catch (IOException e) {
    e.printStackTrace();
    }
    }

    }

    private static  void refreshPoint(long p) {  //更新指针读取位置
    PrintWriter pw=null;
    try{
     pw = new PrintWriter(new OutputStreamWriter(new FileOutputStream(path)));
     pw.println(p);
                  //System.out.println("保存指针位置完毕!!!!!!"+ p);
                  //System.out.println();
     pw.flush();
     pw.close();
    }catch(Exception e){
    e.printStackTrace();
    }
    }
        //解析上次没有下线的人的信息
    public class ParseUpset extends Thread {    
    @Override
        public void run(){
    long start = System.currentTimeMillis();
    parse();
    long end = System.currentTimeMillis();
        System.out.println("采集updata时间:"+(end - start)/1000+" s, "+(end - start)%1000+" ms");                    
        }
        public void parse(){
       //解析up文件中的上线的文件的数据
                                        //System.out.println("正在采集没下线信息");
    String ip=null;
    String newIp= null;
                Set<String> ips = upMap.keySet();
    for(String data:ips){
    //拿到upset当中没有下线的
    ip = data;
    String str1 = upMap.get(ip);
    String[] s1= str1.split("\\|");// System.out.println("解析:"+ip);

    Set<String> allSet = allMap.keySet();
    Iterator<String> iter = allSet.iterator();
    while(iter.hasNext()){
           newIp = (String) iter.next();                                                                                                   
                                   // System.out.println(s2.);
           if(ip.equals(newIp)){         
            
            String str2 = allMap.get(ip);
           String[] s2= str2.split("\\|");
           
            if("8".equals(s2[2])){
         BIDR bidr = new BIDR();
         bidr.setAAA_login_name(s2[0]);
         bidr.setNAS_ip(s2[1]);
         bidr.setLogin_ip(ip);
         bidr.setLogin_date(new Timestamp(Long.parseLong(s1[1])));
         bidr.setLogout_date(new Timestamp(Long.parseLong(s2[3])));
         long t = Long.parseLong(s2[3])-Long.parseLong(s1[1]);
         int time = (int)(t%60000==0 ? (int)t/60000 : (int)t/60000+1);
         bidr.setTime_deration(time);
                                                    //System.out.println("找到下下线信息:");
                                bidrSet1.add(bidr);          //System.out.println("find:"+bidrSet1.size());
                                iter.remove();               //删除迭代器当前值,提高效率             
                                //upMap.remove(newIp);           //迭代器失败,无法移除以下线的信息
                                //upSet.remove(str);                          
                                break;
           }
           }
           
      }
       }
    }     
      }
    }
      

  5.   

    public class ParseFile extends Thread {
     long start;
     
     public void run(){
     try{
     start = System.currentTimeMillis();
     refreshPoint(point);                    
     long start = System.currentTimeMillis();
     parseFile();
     long end = System.currentTimeMillis();
                                            System.out.println("采集file时间:"+(end - start)/1000+" s, "+(end - start)%1000+" ms");
                                            System.out.println();
     }catch(Exception e){
     e.printStackTrace();
     refreshPoint(prePoint);
     }
     }

      public  void parseFile(){
    try{
    if(point==allMap.size()) return;
                                                    
                // System.out.println("正在采集新数据");
    String ip=null;
        //解析文件中的信息模块
    Set<String> set = pointMap.keySet();                //System.out.println("pointMap: "+pointMap.size());
    for(String ips:set){
    String data = pointMap.get(ips);
    String s1[] = data.split("\\|");
                                               //System.out.println(allMap.get(ips));                              
    ip = ips;                              //System.out.println("正在解析:"+ip);
    String ipss = allMap.get(ips);          
    if(ipss!=null){            
                 String s2[] = ipss.split("\\|");
                 
                 BIDR bidr = new BIDR();
         bidr.setAAA_login_name(s1[0]);
         bidr.setNAS_ip(s1[1]);
         bidr.setLogin_ip(ip);
         bidr.setLogin_date(new Timestamp(Long.parseLong(s1[3])));
         bidr.setLogout_date(new Timestamp(Long.parseLong(s2[3])));
         long t = Long.parseLong(s2[3])-Long.parseLong(s1[3]);
         int time = (int)(t%60000==0 ? (int)t/60000 : (int)t/60000+1);
         bidr.setTime_deration(time);
        
                                bidrSet2.add(bidr); 
                                find++; 
                                /*preSize = bidrSet.size();
         size = preSize;
                                size = bidrSet.size();*/
                                flag = true;                                                         
                                continue;
    }
                                            
       if(flag!=true){
    upMap.put(ips,pointMap.get(ips));   //System.out.println("添加没下线信息成功");
    length = upMap.size();
       }  
       }                 flag1 = false;              //解析完毕,监听没下线线程关闭
                    // System.out.println("解析完毕!!!!!!!!!!!!!!");
                    // System.out.println();
                     //System.out.println("find: "+find);   }catch(Exception e){
      //refresh(prePoint);             //更新信息处理初始位置
      }
    }
     } //将本次没有下线的信息保存到文件中
    public class refreshUpData extends Thread{              
    @Override
    public void run(){
                             // System.out.println("正在监听并保存解析后没下线的数据。。"); 
     String path = prop.getProperty("up-file");
     while(flag1){
        if(preLength!=length){                           // System.out.println(path);
    try {
    ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path));
    Set<String> set = (Set)upMap.values();
    for(String str:set){
    oos.writeObject(str);
    }
    //System.out.println("正在监听。。"); 
    } catch (FileNotFoundException e) {
    e.printStackTrace();
    } catch (IOException e) {
    e.printStackTrace();
    }
                                // System.out.println("保存完毕。。"); 
    try {
    sleep(1000);             //睡眠一秒钟确保解析文件线程完全采集完毕,以备保存本次文件中未下线信息
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
      }
      }  
     
       }
    }
      

  6.   

    看看楼上的代码,只不过解析的格式为
    #briup1660|037:wKgB1660A|7|1239110900|44.211.221.247
    其实一个样
      

  7.   

    您说的这三步我都完成了,关键最后怎么执行insert into Report_Links_LX() values(?,?,?,?,?)
    我这个五个字段的下标怎么取?