我的 CSV 文件如下所示:
我想解析以下 CSV 文件:
Entity,GeographicLocation(Headers)
coffee service,"San Antonio, TX Metro"
coffee service,"Honolulu, HI Metro"
coffee service,"Little Rock, AR Metro"
coffee service,"Jacksonville, FL Metro"
coffee service,"Lincoln, NE Metro"
我想得到这个表格的字段:(期望的输出)
coffee+service,San+Antonio+TX+Metro
coffee+service,Honolulu+HI+Metro
coffee+service,Little+Rock+AR+Metro and so on ...
我的代码如下所示:
// String CSVfilename = args[0];
String csvfile="/Users/cdas/databaseprograms/coffeeservice.csv";
// Step 1 >
// code to read the data from the CSV file
BufferedReader br=new BufferedReader(new FileReader(csvfile));
StringTokenizer st=null;
String line="";
int linenumber=0;
int columnnumber;
int free=0;
int free1=0;
// create the arraylist
ArrayList<String> Typeof = new ArrayList<String>();
// ArrayList for the adgroupId
ArrayList<String> Where = new ArrayList<String>();
// reading from the CSV file
while((line=br.readLine())!=null){
linenumber++;
columnnumber=0;
st=new StringTokenizer(line,",");
while(st.hasMoreTokens()){
columnnumber++;
String token=st.nextToken();
token = token.replaceAll("\"","");
token = token.replaceAll(",","");
System.out.println(token);
if("Entity".equals(token)){
free=columnnumber;
System.out.println("the value of free"+free);
} else if("GeographicLocation".equals(token)){
free1=columnnumber;
System.out.println("the value of free1"+free1);
}
if(linenumber>1){
if (columnnumber==free){
token = token.replaceAll(" ","+");
Typeof.add(token);
} else if(columnnumber==free1){
//token = token.replaceAll(",","+");
token = token.replaceAll(" ","+");
Where.add(token);
}
}
}
}
// converting the headline arraylist to array
String[] entity=Typeof.toArray(new String[Typeof.size()]);
for(int i=0;i<entity.length;i++){
System.out.println(entity[i]);}
// converting the keyword Id arraylist to array
String[] whereof=Where.toArray(new String[Where.size()]);
for(int i=0;i<whereof.length;i++){
System.out.println(whereof[i]);
}
My Output is like : -
Entity
the value of free1
GeographicLocation
the value of free12
coffee service
San Antonio
TX Metro
coffee service
Honolulu
HI Metro
coffee service
Little Rock
AR Metro
coffee service
Jacksonville
FL Metro
coffee service
Lincoln
NE Metro
我已成功替换双引号,但 bufferedReader 以我不想要的逗号转义。请帮我解决以下问题。