[导入]mysql 导出文本数据转换展现格式



package  uu;

import  java.io.BufferedReader;
import  java.io.File;
import  java.io.FileInputStream;
import  java.io.FileOutputStream;
import  java.io.InputStreamReader;
import  java.io.OutputStreamWriter;
import  java.util.ArrayList;
import  java.util.HashMap;
import  java.util.List;
import  java.util.Map;
import  java.util.regex.Pattern;

public   class  MainExc {
    
// 数据文本 
     static  String filePath  =   " smsservice.data " ;
    
// 输出文本
     static  String outfile  =   " outfile.html " ;
    
// 输出 文本格式
     static  String charset  =   " GBK " ;

    
// 数据逻辑 展现
     static  Map < Pattern, String >  map  =   new  HashMap < Pattern, String > ();
    
static  {
        map.put(Pattern.compile(
" ^XZT " ),  " sina " );
        。。。。。
    }

    
// 类型定义
     static  Map < Pattern, String >  maptype  =   new  HashMap < Pattern, String > ();
    
static  {
        maptype.put(Pattern.compile(
" ^3P45 " ),  " 客户端 " );
        maptype.put(Pattern.compile(
" ^6930 " ),  " 图表 " );
        ......

    }

    
// 禁止展现
     static  List < Pattern >  listNo  =   new  ArrayList < Pattern > ();
    
static  {
        listNo.add(Pattern.compile(
" ^9588Command$ " ));
        listNo.add(Pattern.compile(
" ^5kjsCommand$ " ));
        listNo.add(Pattern.compile(
" ^529901001011021$ " ));
        listNo.add(Pattern.compile(
" ^529901001011022$ " ));
    }

    
    
    
public   static   void  main(String[] args)  throws  Exception {
        
//  read
        InputStreamReader read  =   new  InputStreamReader(
                
new  FileInputStream(MainExc. class .getClassLoader().getResource(
                        filePath).getPath()), charset);
        
        
//  writer
         if  (MainExc. class .getClassLoader().getResource(outfile)  ==   null ) {
            
new  File(MainExc. class .getClassLoader().getResource( " . " ).getPath()
                    
+   " /outfile.html " ).createNewFile();
        }
        OutputStreamWriter writer 
=   new  OutputStreamWriter(
                
new  FileOutputStream(MainExc. class .getClassLoader()
                        .getResource(outfile).getPath()), charset);
        
        
//  内存 装载 处
        List < Map < String, String >>  datas  =   new  ArrayList < Map < String, String >> ();

        
        
        StringBuffer buffer 
=   new  StringBuffer();
        BufferedReader reader 
=   new  BufferedReader(read);
        String stmp 
=   null ;
        List
< String >  colName  =   new  ArrayList < String > ();
        
// 得到 列名
         if  ((stmp  =  reader.readLine())  !=   null   &&   ! stmp.trim().equals( "" )) {
            
//  split - > Tab
             for  (String stmp2 : stmp.split( "      " )) {
                colName.add(stmp2);
            }
        }
        
        
// 列数据收集
         while  ((stmp  =  reader.readLine())  !=   null   &&   ! stmp.trim().equals( "" )) {

            Map
< String, String >  data  =   new  HashMap < String, String > ();
            String[] sdata 
=  stmp.split( "      " );
            
for  ( int  i  =   0 ; i  <  colName.size(); i ++ ) {
                data.put(colName.get(i), sdata[i]);
            }
            datas.add(data);
        }

        
        
//  writer
        writer.append( " <center><table  border>\n " );
        writer.append(
" <tr> "   +   " <td>通道伙伴</td> "   +   " <td>指令</td> "   +   " <td>长号码</td> "
                
+   " <td>资费</td> "   +   " <td>业务类型(图表/客户端)</td> "   +   " </tr>\n " );
        
//  通道伙伴 指令 长号码 资费 业务类型(图表?客户端?)
        wfor:  for  (Map < String, String >  mtmp : datas) {

            
for  (Pattern ptmp : listNo) {
                
if  (ptmp.matcher(mtmp.get( " command " )).find()) {
                    
continue  wfor;
                }
            }

            String hzhb 
=   "" ;

            
for  (Pattern ptmp : map.keySet()) {
                
if  (ptmp.matcher(mtmp.get( " command " )).find()) {
                    hzhb 
=  map.get(ptmp);
                }
            }

            String type 
=   "" ;
            
for  (Pattern ptmp : maptype.keySet()) {
                
if  (ptmp.matcher(mtmp.get( " command " )).find()) {
                    type 
=  maptype.get(ptmp);
                }
            }

            writer.append(
" <tr> "   +   " <td align='center'> "   +  hzhb  +   " </td> "
                    
+   " <td align='center'> "   +  mtmp.get( " command " +   " </td> "
                    
+   " <td align='center'> "   +  mtmp.get( " num " +   " </td> "
                    
+   " <td align='center'> "   +  mtmp.get( " money " +   " </td> "
                    
+   " <td align='center'> "   +  type  +   " </td> "   +   " </tr>\n " );
        }
        writer.append(
" </table></center>\n " );

        
//  save 
        writer.flush();
        writer.close();
    }

}



G_G 2008-07-25 16:39 发表评论

文章来源:http://www.blogjava.net/Good-Game/archive/2008/07/25/217511.html
posted @ 2008-07-26 10:33  阿舟  阅读(550)  评论(0编辑  收藏  举报