java 學(xué)習(xí)

          軟件開發(fā)相關(guān)方面的學(xué)習(xí)

          BlogJava 首頁 新隨筆 聯(lián)系 聚合 管理
            33 Posts :: 1 Stories :: 5 Comments :: 0 Trackbacks

          testSplitCSV.java:

          import java.util.Vector;
          class  testSplitCSV{
           /**
           * Split one line of csv file
           * @return a String array results
           */
           public static String[] splitCSV(String src) throws Exception{
            if (src==null || src.equals("")) return new String[0];
            StringBuffer st=new StringBuffer();
            Vector result=new Vector();
            boolean beginWithQuote = false;
            for (int i=0;i<src.length();i++){
             char ch = src.charAt(i);
             if (ch=='\"'){
              if (beginWithQuote){
               i++;
               if (i>=src.length()){
                result.addElement(st.toString());
                st=new StringBuffer();
                beginWithQuote=false;
               }else{
                ch=src.charAt(i);
                if (ch == '\"'){
                 st.append(ch);
                }else if (ch == ','){
                 result.addElement(st.toString());
                 st=new StringBuffer();
                 beginWithQuote = false;
                }else{
                 throw new Exception("Single double-quote char mustn't exist in filed "+(result.size()+1)+" while it is begined with quote\nchar at:"+i);
                }
               }
              }else if (st.length()==0){
               beginWithQuote = true;
              }else{
               throw new Exception("Quote cannot exist in a filed which doesn't begin with quote!\nfield:"+(result.size()+1));
              }
             }else if (ch==','){
              if (beginWithQuote){
               st.append(ch);
              }else{
               result.addElement(st.toString());
               st=new StringBuffer();
               beginWithQuote = false;
              }
             }else{
              st.append(ch);
             }
            }
            if (st.length()!=0){
             if (beginWithQuote){
              throw new Exception("last field is begin with but not end with double quote");
             }else{
              result.addElement(st.toString());
             }
            }
            String rs[] = new String[result.size()];
            for (int i=0;i<rs.length;i++){
             rs[i]=(String)result.elementAt(i);
            }
           return rs;
           }

           public static void main(String[] args){
            String src1=  "\"fh,zg\",sdf,\"asfs,\",\",dsdf\",\"aadf\"\"\",\"\"\"hdfg\",\"fgh\"\"dgnh\",hgfg'dfh,\"asdfa\"\"\"\"\",\"\"\"\"\"fgjhg\",\"gfhg\"\"\"\"hb\"";
            try {
             String[] Ret = splitCSV(src1);
             for (int i=0;i<Ret.length;i++){
              System.out.println(i+": "+Ret[i]);
             }
            }
            catch(Exception e) {
             e.printStackTrace();
            }
           }
          }

          posted on 2006-02-27 17:28 zxf 閱讀(2020) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發(fā)表評論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 乐清市| 同德县| 四平市| 尼木县| 潞西市| 崇礼县| 金川县| 前郭尔| 迁安市| 凌源市| 麻城市| 鸡泽县| 山西省| 陵水| 林西县| 黔西| 德庆县| 正安县| 东安县| 伊金霍洛旗| 台州市| 阿合奇县| 古浪县| 伊川县| 崇文区| 即墨市| 昌平区| 遂昌县| 华蓥市| 舒兰市| 凭祥市| 北宁市| 周宁县| 楚雄市| 内丘县| 扎鲁特旗| 福安市| 南乐县| 青浦区| 新宁县| 万载县|