我有2个带有条目列表的文件,第一个文件有5个项目并且由';;'分开,第二个文件只有1个条目。(注意*两个文件的行数相同。)我想要两者结合将文件一起归档到单个arraylist并将其重新写入新文件。我该怎么办?
while ((line = br.readLine()) != null) {
String[] items = line.split(";;");
list.add(items[1]+";;" + items[2] + ";;" + items[3] + ";;" + items[4] + ";;" + items[5]);
}
while ((line = br.readLine()) != null) {
list2.add(line);
}
null;;13;;male;;USA;;single;;address
null;;12;;female;;USA;;single;;address
jack
mary
jack;;13;;male;;USA;;single;;address
答案 0 :(得分:1)
对两个文件使用两个bufferedeader
while ((line1 = br1.readLine()) != null && (line2=br2.readLine())!=null) {
String[] items = line1.split(";;");
String firstName=line2;
list.add(firstName+";;"+items[1]+";;" + items[2] + ";;" + items[3] + ";;" + items[4] + ";;" + items[5]);
}
答案 1 :(得分:0)
好吧,如果您需要List
包含;;
- 第一个列表中的分隔项+ ;;
以及第二个列表中同一行中的项目,这是解决方案:
String lineFromFirst, lineFromSecond;
while ((lineFromFirst = br.readLine()) != null) {
String[] items = lineFromFirst .split(";;");
lineFromSecond = brForSecondFile.readLine();
//I assume no checks are neccesary as you wrote that the files has equal number of lines
list.add(lineFromSecond + ";;" + items[1]+";;" + items[2] + ";;" + items[3] + ";;" + items[4] + ";;" + items[5]);
}