我在包含多行的文件中有csv。如果第一列的值没有任何错误,并且m不能在数据库中插入。 ex 如果行是:130,1,datafile8.csv,2007,17,List_date在读取n时没有问题插入 但是如果行是:,0,datafile8.csv,Bihar,7,list_Left,无法读取n插入。如何在上面的行中插入null。因此,我可以在数据库中插入dis行。java代码如何读取csv文件中的空值
String keyword = "celldescription.csv";
File makefile = new File(keyword);
BufferedReader r2 = new BufferedReader(new FileReader(makefile));
strLine1 = r2.readLine();
System.out.println (strLine1);
String r="0";int r1=0;
while((strLine1=r2.readLine())!=null)
{
System.out.println (strLine1);
StringTokenizer st2 = new StringTokenizer(strLine1, ",");
// Print the content on the console
String cellvalue = st2.nextToken();
String position = st2.nextToken();
String Docid=st2.nextToken();
String Word=st2.nextToken();
String Count=st2.nextToken();
String List_Entry=st2.nextToken();
String tab3="insert into description(cellvalue,position,Docid,Word,Count,List_Entry) values(?,?,?,?,?,?)";
ps = connection.prepareStatement(tab3);
ps.setString (1,cellvalue);
ps.setString (2,position);
ps.setString (3,Docid);
ps.setString (4,Word);
ps.setString (5,Count);
ps.setString (6,List_Entry);
ps.executeUpdate();
}//end of while
r2.close();
System.out.println("Data is inserted");
}//try closed**
我还没有在代码中看到'StringTokenizer'很长一段时间。你可能要考虑使用'String.split(“,”)'代替。 – Perception 2013-02-15 10:09:24
添加空检查很困难吗?嗯.. – StarPinkER 2013-02-15 10:10:04
此外,CSV看起来相当复杂(引用的值包括分隔符等)。我建议使用第三方API,Apache有一个看起来不错的。 – SJuan76 2013-02-15 10:10:52