Java拆分数据csv文件

时间:2017-05-02 15:05:17

标签: java arrays split

我将csv文件整理为{Id,OwnerUserId,CreationDate,ClosedDate,Score,Title,Body}

我正在尝试使用特殊字符拆分数据。这里的例子是我的数据:

 1040,254,2008-08-04T05:45:22Z,NA,42,How do I delete a file which is locked by another process in C#?,"<p>I'm looking for a way to delete a file which is locked by another process using C#. I suspect the method must be able to find which process is locking the file (perhaps by tracking the handles, although I'm not sure how to do this in C#) then close that process before being able to complete the file delete using <code>File.Delete()</code>.</p>
"
1070,236,2008-08-04T07:34:44Z,NA,17,Process size on UNIX,"<p>What is the correct way to get the process size on <code>Solaris, HP-UX</code> and <code>AIX</code>? Should we use <code>top</code> or <code>ps -o vsz</code> or something else?</p>

似乎数据用逗号分隔,但有时候标题或正文也包含逗号,并且似乎它“在被记录之间。所以我怎样才能达到以下结果:

Array 1 { 1040,254,2008-08-04T05:45:22Z,NA,42, title data, body data }
Array 2 { 1070,236,2008-08-04T07:34:44Z,NA,17, title data, body data }

我试过了

String[] tokens = line.split(",(?![^<>]*</>)") 

但没有效果

1 个答案:

答案 0 :(得分:0)

您应该使用一些CSV解析库。

你遇到的问题不是最后一个问题。您知道CSV记录并不总是只包含一个字符串,或者"符号有特殊的转义序列吗?