我将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
我希望输出像表格格式
答案 0 :(得分:1)
你只需要添加
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+" ")
开始,直到您决定了您想要的确切格式。
答案 1 :(得分:0)
试试这个
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.List;
public class tsv_read1{
public static void main(String[] arg) throws Exception {
BufferedReader TSVFile = new BufferedReader(new FileReader("tsvfile.tsv"));
List<String>dataArray = new ArrayList<String>() ;
String dataRow = TSVFile.readLine();
while (dataRow != null){
dataArray.add(dataRow);
dataRow = TSVFile.readLine();
}
TSVFile.close();
for(String item:dataArray){
System.out.println(item);
}
}
}