我的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正在以我不想要的逗号进行转义。请帮我处理以下内容。
答案 0 :(得分:1)
String str = "coffee service,\"San Antonio, TX Metro\"";
System.out.println("str = " + str.replaceAll("\"","")
.replaceAll(", "," ")
.replaceAll(" ","+"));
<强>输出:强>
str = coffee+service,San+Antonio+TX+Metro