我将tsv文件的数据添加到列表中。但是它反复提供数据。
这是tsv文件
UserID City State Country ZipCode DegreeType Major GraduationDate WorkHistoryCount TotalYearsExperience CurrentlyEmployed ManagedOthers ManagedHowMany
47 Paramount CA US 90723 High School 6/1/1999 0:00 3 10 Yes No 0
72 La Mesa CA US 91941 Master's Anthropology 1/1/2011 0:00 10 8 Yes No 0
80 Williamstown NJ US 8094 High School Not Applicable 6/1/1985 0:00 5 11 Yes Yes 5
98 Astoria NY US 11105 Master's Journalism 5/1/2007 0:00 3 3 Yes No 0
这是我的代码
public class tsv_read{
public static void main(String[] arg) throws Exception {
BufferedReader TSVFile =
new BufferedReader(new FileReader("tsvfile.tsv"));
String dataRow = TSVFile.readLine();
List<String> list = new ArrayList<String>();
while (dataRow != null){
String[] dataArray = dataRow.split("\t");
for (String item:dataArray) {
list.add(item);
}
Iterator<String> it = list.iterator();
while (it.hasNext()) {
String txt = it.next();
System.out.print(txt);
}
System.out.println(); // Print the data line.
dataRow = TSVFile.readLine();
}
TSVFile.close();
System.out.println();
} //main()
}
这是我得到的输出
UserIDCityStateCountryZipCodeDegreeTypeMajorGraduationDateWorkHistoryCountTotalYearsExperienceCurrentlyEmployedManagedOthersManagedHowMany
UserIDCityStateCountryZipCodeDegreeTypeMajorGraduationDateWorkHistoryCountTotalYearsExperienceCurrentlyEmployedManagedOthersManagedHowMany47ParamountCAUS90723High School6/1/1999 0:00310YesNo0
UserIDCityStateCountryZipCodeDegreeTypeMajorGraduationDateWorkHistoryCountTotalYearsExperienceCurrentlyEmployedManagedOthersManagedHowMany47ParamountCAUS90723High School6/1/1999 0:00310YesNo072La MesaCAUS91941Master'sAnthropology1/1/2011 0:00108YesNo0
UserIDCityStateCountryZipCodeDegreeTypeMajorGraduationDateWorkHistoryCountTotalYearsExperienceCurrentlyEmployedManagedOthersManagedHowMany47ParamountCAUS90723High School6/1/1999 0:00310YesNo072La MesaCAUS91941Master'sAnthropology1/1/2011 0:00108YesNo080WilliamstownNJUS8094High SchoolNot Applicable6/1/1985 0:00511YesYes5
UserIDCityStateCountryZipCodeDegreeTypeMajorGraduationDateWorkHistoryCountTotalYearsExperienceCurrentlyEmployedManagedOthersManagedHowMany47ParamountCAUS90723High School6/1/1999 0:00310YesNo072La MesaCAUS91941Master'sAnthropology1/1/2011 0:00108YesNo080WilliamstownNJUS8094High SchoolNot Applicable6/1/1985 0:00511YesYes598AstoriaNYUS11105Master'sJournalism5/1/2007 0:0033YesNo0
我想要像表格格式的输出
最佳答案
您只需要添加
list.clear();
在每次运行循环的开始时。
while (dataRow != null){
list.clear();
String[] dataArray = dataRow.split("\t");
// etc.
目前发生的事情是您的
List
越来越大...在第一行中,您将添加该行中的所有字段,然后在第二行中,将所有这些字段添加至末尾,依此类推。 。,但您永远不会清空列表并重新开始。另一种解决方案是在循环内移动
List
声明:String dataRow = TSVFile.readLine();
while (dataRow != null){
List<String> list = new ArrayList<String>();
String[] dataArray = dataRow.split("\t");
// etc.
这样,您每次都会得到一个新列表。
顺便说一句,如果您想知道为什么要打印的字段都一起运行,那是因为您反复调用
System.out.print(txt)
而没有任何分隔符。您可能要从System.out.print(txt+" ")
开始,直到您确定所需的确切格式。关于java - 如何使用集合读取和显示csv文件,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/26462273/