iamsheny 发表于 2013-1-27 04:52:46

java 讀取 csv 檔

import java.io.BufferedReader;import java.io.FileReader;import java.io.IOException;import java.util.StringTokenizer;public class CsvFileImport extends AbstractAction{    private static final long serialVersionUID = 1597014893653111623L;    private static String line = null;    String fileName = "C:\\javatest\\CSVImport.csv";    @Override    public String execute() throws Exception {      return SUCCESS;    }      public void importdata() throws Exception {      BufferedReader in = new BufferedReader(new FileReader(fileName));            try{      while((line = in.readLine()) != null){            StringTokenizer st=new StringTokenizer(line,",");            int tokenCount=0;            while(st.hasMoreTokens()){                tokenCount++;                String token=st.nextToken();                if(token.indexOf("\"")!=-1){                  //this token contains a " , so we should read the next one too                  String next;                  while((next=st.nextToken()).indexOf("\"")==-1)                         token+=next;//ugly,might use a stringbuffer                  //it might also break if csv incorrectly formed                }                System.out.println(tokenCount+"============="+token);                //you now have your token here, do extra processing with it            }      }      }      catch(IOException e)      {            e.printStackTrace();      }    }}
页: [1]
查看完整版本: java 讀取 csv 檔